diff options
Diffstat (limited to 'common')
52 files changed, 0 insertions, 17596 deletions
diff --git a/common/Makefile.am b/common/Makefile.am deleted file mode 100644 index b053ec0..0000000 --- a/common/Makefile.am +++ /dev/null @@ -1,111 +0,0 @@ - -inc_HEADERS += \ - common/pkcs11.h \ - common/pkcs11x.h \ - $(NULL) - -noinst_LTLIBRARIES += \ - libp11-common.la \ - libp11-library.la \ - libp11-test.la \ - libp11-tool.la \ - $(NULL) - -libp11_common_la_SOURCES = \ - common/argv.c common/argv.h \ - common/attrs.c common/attrs.h \ - common/array.c common/array.h \ - common/buffer.c common/buffer.h \ - common/compat.c common/compat.h \ - common/constants.c common/constants.h \ - common/debug.c common/debug.h \ - common/dict.c common/dict.h \ - common/hash.c common/hash.h \ - common/lexer.c common/lexer.h \ - common/message.c common/message.h \ - common/path.c common/path.h \ - common/pkcs11.h common/pkcs11x.h common/pkcs11i.h \ - common/url.c common/url.h \ - $(NULL) - -libp11_library_la_SOURCES = \ - common/library.c common/library.h \ - $(NULL) - -libp11_test_la_SOURCES = \ - common/mock.c common/mock.h \ - common/test.c common/test.h \ - $(NULL) - -libp11_tool_la_SOURCES = \ - common/tool.c common/tool.h \ - $(NULL) - -# Tests ---------------------------------------------------------------- - -common_LIBS = \ - libp11-test.la \ - libp11-common.la \ - $(NULL) - -CHECK_PROGS += \ - test-tests \ - test-compat \ - test-hash \ - test-dict \ - test-array \ - test-constants \ - test-attrs \ - test-buffer \ - test-url \ - test-path \ - test-lexer \ - test-message \ - $(NULL) - -test_array_SOURCES = common/test-array.c -test_array_LDADD = $(common_LIBS) - -test_attrs_SOURCES = common/test-attrs.c -test_attrs_LDADD = $(common_LIBS) - -test_buffer_SOURCES = common/test-buffer.c -test_buffer_LDADD = $(common_LIBS) - -test_compat_SOURCES = common/test-compat.c -test_compat_LDADD = $(common_LIBS) - -test_constants_SOURCES = common/test-constants.c -test_constants_LDADD = $(common_LIBS) - -test_dict_SOURCES = common/test-dict.c -test_dict_LDADD = $(common_LIBS) - -test_hash_SOURCES = common/test-hash.c -test_hash_LDADD = $(common_LIBS) - -test_lexer_SOURCES = common/test-lexer.c -test_lexer_LDADD = $(common_LIBS) - -test_message_SOURCES = common/test-message.c -test_message_LDADD = $(common_LIBS) - -test_path_SOURCES = common/test-path.c -test_path_LDADD = $(common_LIBS) - -test_tests_SOURCES = common/test-tests.c -test_tests_LDADD = $(common_LIBS) - -test_url_SOURCES = common/test-url.c -test_url_LDADD = $(common_LIBS) - -noinst_PROGRAMS += \ - frob-getauxval \ - frob-getenv \ - $(NULL) - -frob_getauxval_SOURCES = common/frob-getauxval.c -frob_getauxval_LDADD = $(common_LIBS) - -frob_getenv_SOURCES = common/frob-getenv.c -frob_getenv_LDADD = $(common_LIBS) diff --git a/common/argv.c b/common/argv.c deleted file mode 100644 index 6d91bfa..0000000 --- a/common/argv.c +++ /dev/null @@ -1,115 +0,0 @@ -/* - * 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@redhat.com> - */ - -#include "config.h" - -#include "argv.h" -#include "debug.h" - -#include <ctype.h> -#include <stdlib.h> -#include <string.h> - -bool -p11_argv_parse (const char *string, - void (*sink) (char *, void *), - void *argument) -{ - char quote = '\0'; - char *src, *dup, *at, *arg; - bool ret = true; - - return_val_if_fail (string != NULL, false); - return_val_if_fail (sink != NULL, false); - - src = dup = strdup (string); - return_val_if_fail (dup != NULL, false); - - arg = at = src; - for (src = dup; *src; src++) { - - /* Matching quote */ - if (quote == *src) { - quote = '\0'; - - /* Inside of quotes */ - } else if (quote != '\0') { - if (*src == '\\') { - *at++ = *src++; - if (!*src) { - ret = false; - goto done; - } - if (*src != quote) - *at++ = '\\'; - } - *at++ = *src; - - /* Space, not inside of quotes */ - } else if (isspace (*src)) { - *at = 0; - sink (arg, argument); - arg = at; - - /* Other character outside of quotes */ - } else { - switch (*src) { - case '\'': - case '"': - quote = *src; - break; - case '\\': - *at++ = *src++; - if (!*src) { - ret = false; - goto done; - } - /* fall through */ - default: - *at++ = *src; - break; - } - } - } - - - if (at != arg) { - *at = 0; - sink (arg, argument); - } - -done: - free (dup); - return ret; -} diff --git a/common/argv.h b/common/argv.h deleted file mode 100644 index 8f95490..0000000 --- a/common/argv.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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@redhat.com> - */ - -#ifndef P11_ARGV_H_ -#define P11_ARGV_H_ - -#include "compat.h" - -bool p11_argv_parse (const char *string, - void (*sink) (char *, void *), - void *argument); - -#endif /* P11_ARGV_H_ */ diff --git a/common/array.c b/common/array.c deleted file mode 100644 index 185ea2f..0000000 --- a/common/array.c +++ /dev/null @@ -1,129 +0,0 @@ -/* - * 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. - */ - -#include "config.h" - -#include "array.h" -#include "debug.h" - -#include <stdlib.h> -#include <string.h> - -static bool -maybe_expand_array (p11_array *array, - unsigned int length) -{ - unsigned int new_allocated; - void **new_memory; - - if (length <= array->allocated) - return true; - - - new_allocated = array->allocated * 2; - if (new_allocated == 0) - new_allocated = 16; - if (new_allocated < length) - new_allocated = length; - - new_memory = realloc (array->elem, new_allocated * sizeof (void*)); - return_val_if_fail (new_memory != NULL, false); - - array->elem = new_memory; - array->allocated = new_allocated; - return true; -} - -p11_array * -p11_array_new (p11_destroyer destroyer) -{ - p11_array *array; - - array = calloc (1, sizeof (p11_array)); - if (array == NULL) - return NULL; - - if (!maybe_expand_array (array, 2)) { - p11_array_free (array); - return NULL; - } - - array->destroyer = destroyer; - return array; -} - -void -p11_array_free (p11_array *array) -{ - if (array == NULL) - return; - - p11_array_clear (array); - free (array->elem); - free (array); -} - -bool -p11_array_push (p11_array *array, - void *value) -{ - if (!maybe_expand_array (array, array->num + 1)) - return_val_if_reached (false); - - array->elem[array->num] = value; - array->num++; - return true; -} - -void -p11_array_remove (p11_array *array, - unsigned int index) -{ - if (array->destroyer) - (array->destroyer) (array->elem[index]); - memmove (array->elem + index, array->elem + index + 1, - (array->num - (index + 1)) * sizeof (void*)); - array->num--; -} - -void -p11_array_clear (p11_array *array) -{ - unsigned int i; - - if (array->destroyer) { - for (i = 0; i < array->num; i++) - (array->destroyer) (array->elem[i]); - } - - array->num = 0; -} diff --git a/common/array.h b/common/array.h deleted file mode 100644 index 94be29c..0000000 --- a/common/array.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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 Waler <stefw@collabora.co.uk> - */ - -#ifndef __P11_ARRAY_H__ -#define __P11_ARRAY_H__ - -#include "compat.h" - -#ifndef P11_DESTROYER_DEFINED -#define P11_DESTROYER_DEFINED - -typedef void (*p11_destroyer) (void *data); - -#endif - -typedef struct _p11_array { - void **elem; - unsigned int num; - - /* private */ - unsigned int allocated; - p11_destroyer destroyer; -} p11_array; - -p11_array * p11_array_new (p11_destroyer destroyer); - -void p11_array_free (p11_array *array); - -bool p11_array_push (p11_array *array, - void *value); - -void p11_array_remove (p11_array *array, - unsigned int index); - -void p11_array_clear (p11_array *array); - -#endif /* __P11_ARRAY_H__ */ diff --git a/common/attrs.c b/common/attrs.c deleted file mode 100644 index 5a138a8..0000000 --- a/common/attrs.c +++ /dev/null @@ -1,903 +0,0 @@ -/* - * 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> - */ - -#include "config.h" - -#include "attrs.h" -#include "buffer.h" -#include "compat.h" -#include "constants.h" -#include "debug.h" -#include "hash.h" -#include "pkcs11.h" -#include "pkcs11i.h" -#include "pkcs11x.h" - -#include <assert.h> -#include <stdarg.h> -#include <stdio.h> -#include <stdint.h> -#include <stdlib.h> -#include <string.h> - -#define ELEMS(x) (sizeof (x) / sizeof (x[0])) - -bool -p11_attrs_terminator (const CK_ATTRIBUTE *attrs) -{ - return (attrs == NULL || attrs->type == CKA_INVALID); -} - -CK_ULONG -p11_attrs_count (const CK_ATTRIBUTE *attrs) -{ - CK_ULONG count; - - if (attrs == NULL) - return 0UL; - - for (count = 0; !p11_attrs_terminator (attrs); count++, attrs++); - - return count; -} - -void -p11_attrs_free (void *attrs) -{ - CK_ATTRIBUTE *ats = attrs; - int i; - - if (!attrs) - return; - - for (i = 0; !p11_attrs_terminator (ats + i); i++) - free (ats[i].pValue); - free (ats); -} - -static CK_ATTRIBUTE * -attrs_build (CK_ATTRIBUTE *attrs, - CK_ULONG count_to_add, - bool take_values, - bool override, - CK_ATTRIBUTE * (*generator) (void *), - void *state) -{ - CK_ATTRIBUTE *attr; - CK_ATTRIBUTE *add; - CK_ULONG current; - CK_ULONG at; - CK_ULONG j; - CK_ULONG i; - - /* How many attributes we already have */ - current = p11_attrs_count (attrs); - - /* Reallocate for how many we need */ - attrs = realloc (attrs, (current + count_to_add + 1) * sizeof (CK_ATTRIBUTE)); - return_val_if_fail (attrs != NULL, NULL); - - at = current; - for (i = 0; i < count_to_add; i++) { - add = (generator) (state); - - /* Skip with invalid type */ - if (!add || add->type == CKA_INVALID) - continue; - - attr = NULL; - - /* Do we have this attribute? */ - for (j = 0; attr == NULL && j < current; j++) { - if (attrs[j].type == add->type) { - attr = attrs + j; - break; - } - } - - /* The attribute doesn't exist */ - if (attr == NULL) { - attr = attrs + at; - at++; - - /* The attribute exists and we're not overriding */ - } else if (!override) { - if (take_values) - free (add->pValue); - continue; - - /* The attribute exitss, and we're overriding */ - } else { - free (attr->pValue); - } - - memcpy (attr, add, sizeof (CK_ATTRIBUTE)); - if (!take_values && attr->pValue != NULL) { - if (attr->ulValueLen == 0) - attr->pValue = malloc (1); - else - attr->pValue = memdup (attr->pValue, attr->ulValueLen); - return_val_if_fail (attr->pValue != NULL, NULL); - } - } - - /* Mark this as the end */ - (attrs + at)->type = CKA_INVALID; - assert (p11_attrs_terminator (attrs + at)); - return attrs; -} - -static CK_ATTRIBUTE * -vararg_generator (void *state) -{ - va_list *va = state; - return va_arg (*va, CK_ATTRIBUTE *); -} - -CK_ATTRIBUTE * -p11_attrs_build (CK_ATTRIBUTE *attrs, - ...) -{ - CK_ULONG count; - va_list va; - - count = 0UL; - va_start (va, attrs); - while (va_arg (va, CK_ATTRIBUTE *)) - count++; - va_end (va); - - va_start (va, attrs); - attrs = attrs_build (attrs, count, false, true, - vararg_generator, &va); - va_end (va); - - return attrs; -} - -static CK_ATTRIBUTE * -template_generator (void *state) -{ - CK_ATTRIBUTE **template = state; - return (*template)++; -} - -CK_ATTRIBUTE * -p11_attrs_buildn (CK_ATTRIBUTE *attrs, - const CK_ATTRIBUTE *add, - CK_ULONG count) -{ - return attrs_build (attrs, count, false, true, - template_generator, &add); -} - -CK_ATTRIBUTE * -p11_attrs_take (CK_ATTRIBUTE *attrs, - CK_ATTRIBUTE_TYPE type, - CK_VOID_PTR value, - CK_ULONG length) -{ - CK_ATTRIBUTE attr = { type, value, length }; - CK_ATTRIBUTE *add = &attr; - return attrs_build (attrs, 1, true, true, - template_generator, &add); -} - -CK_ATTRIBUTE * -p11_attrs_merge (CK_ATTRIBUTE *attrs, - CK_ATTRIBUTE *merge, - bool replace) -{ - CK_ATTRIBUTE *ptr; - CK_ULONG count; - - if (attrs == NULL) - return merge; - - ptr = merge; - count = p11_attrs_count (merge); - - attrs = attrs_build (attrs, count, true, replace, - template_generator, &ptr); - - /* - * Since we're supposed to own the merge attributes, - * free the container array. - */ - free (merge); - - return attrs; -} - -CK_ATTRIBUTE * -p11_attrs_dup (const CK_ATTRIBUTE *attrs) -{ - CK_ULONG count; - - count = p11_attrs_count (attrs); - return p11_attrs_buildn (NULL, attrs, count); -} - -CK_ATTRIBUTE * -p11_attrs_find (CK_ATTRIBUTE *attrs, - CK_ATTRIBUTE_TYPE type) -{ - CK_ULONG i; - - for (i = 0; !p11_attrs_terminator (attrs + i); i++) { - if (attrs[i].type == type) - return attrs + i; - } - - return NULL; -} - -CK_ATTRIBUTE * -p11_attrs_findn (CK_ATTRIBUTE *attrs, - CK_ULONG count, - CK_ATTRIBUTE_TYPE type) -{ - CK_ULONG i; - - for (i = 0; i < count; i++) { - if (attrs[i].type == type) - return attrs + i; - } - - return NULL; -} - -bool -p11_attrs_find_bool (const CK_ATTRIBUTE *attrs, - CK_ATTRIBUTE_TYPE type, - CK_BBOOL *value) -{ - CK_ULONG i; - - for (i = 0; !p11_attrs_terminator (attrs + i); i++) { - if (attrs[i].type == type && - attrs[i].ulValueLen == sizeof (CK_BBOOL) && - attrs[i].pValue != NULL) { - *value = *((CK_BBOOL *)attrs[i].pValue); - return true; - } - } - - return false; -} - -bool -p11_attrs_findn_bool (const CK_ATTRIBUTE *attrs, - CK_ULONG count, - CK_ATTRIBUTE_TYPE type, - CK_BBOOL *value) -{ - CK_ULONG i; - - for (i = 0; i < count; i++) { - if (attrs[i].type == type && - attrs[i].ulValueLen == sizeof (CK_BBOOL) && - attrs[i].pValue != NULL) { - *value = *((CK_BBOOL *)attrs[i].pValue); - return true; - } - } - - return false; -} - -bool -p11_attrs_find_ulong (const CK_ATTRIBUTE *attrs, - CK_ATTRIBUTE_TYPE type, - CK_ULONG *value) -{ - CK_ULONG i; - - for (i = 0; !p11_attrs_terminator (attrs + i); i++) { - if (attrs[i].type == type && - attrs[i].ulValueLen == sizeof (CK_ULONG) && - attrs[i].pValue != NULL) { - *value = *((CK_ULONG *)attrs[i].pValue); - return true; - } - } - - return false; -} - -bool -p11_attrs_findn_ulong (const CK_ATTRIBUTE *attrs, - CK_ULONG count, - CK_ATTRIBUTE_TYPE type, - CK_ULONG *value) -{ - CK_ULONG i; - - for (i = 0; i < count; i++) { - if (attrs[i].type == type && - attrs[i].ulValueLen == sizeof (CK_ULONG) && - attrs[i].pValue != NULL) { - *value = *((CK_ULONG *)attrs[i].pValue); - return true; - } - } - - return false; -} - -void * -p11_attrs_find_value (CK_ATTRIBUTE *attrs, - CK_ATTRIBUTE_TYPE type, - size_t *length) -{ - CK_ULONG i; - - for (i = 0; !p11_attrs_terminator (attrs + i); i++) { - if (attrs[i].type == type && - attrs[i].ulValueLen != 0 && - attrs[i].ulValueLen != (CK_ULONG)-1 && - attrs[i].pValue != NULL) { - if (length) - *length = attrs[i].ulValueLen; - return attrs[i].pValue; - } - } - - return NULL; -} - -CK_ATTRIBUTE * -p11_attrs_find_valid (CK_ATTRIBUTE *attrs, - CK_ATTRIBUTE_TYPE type) -{ - CK_ULONG i; - - for (i = 0; !p11_attrs_terminator (attrs + i); i++) { - if (attrs[i].type == type && - attrs[i].pValue != NULL && - attrs[i].ulValueLen != 0 && - attrs[i].ulValueLen != (CK_ULONG)-1) - return attrs + i; - } - - return NULL; -} - -bool -p11_attrs_remove (CK_ATTRIBUTE *attrs, - CK_ATTRIBUTE_TYPE type) -{ - CK_ULONG count; - CK_ULONG i; - - count = p11_attrs_count (attrs); - for (i = 0; i < count; i++) { - if (attrs[i].type == type) - break; - } - - if (i == count) - return false; - - if (attrs[i].pValue) - free (attrs[i].pValue); - - memmove (attrs + i, attrs + i + 1, (count - (i + 1)) * sizeof (CK_ATTRIBUTE)); - attrs[count - 1].type = CKA_INVALID; - return true; -} - -void -p11_attrs_purge (CK_ATTRIBUTE *attrs) -{ - int in, out; - - for (in = 0, out = 0; !p11_attrs_terminator (attrs + in); in++) { - if (attrs[in].ulValueLen == (CK_ULONG)-1) { - free (attrs[in].pValue); - attrs[in].pValue = NULL; - attrs[in].ulValueLen = 0; - } else { - if (in != out) - memcpy (attrs + out, attrs + in, sizeof (CK_ATTRIBUTE)); - out++; - } - } - - attrs[out].type = CKA_INVALID; - assert (p11_attrs_terminator (attrs + out)); - -} - -bool -p11_attrs_match (const CK_ATTRIBUTE *attrs, - const CK_ATTRIBUTE *match) -{ - CK_ATTRIBUTE *attr; - - for (; !p11_attrs_terminator (match); match++) { - attr = p11_attrs_find ((CK_ATTRIBUTE *)attrs, match->type); - if (!attr) - return false; - if (!p11_attr_equal (attr, match)) - return false; - } - - return true; -} - -bool -p11_attrs_matchn (const CK_ATTRIBUTE *attrs, - const CK_ATTRIBUTE *match, - CK_ULONG count) -{ - CK_ATTRIBUTE *attr; - CK_ULONG i; - - for (i = 0; i < count; i++) { - attr = p11_attrs_find ((CK_ATTRIBUTE *)attrs, match[i].type); - if (!attr) - return false; - if (!p11_attr_equal (attr, match + i)) - return false; - } - - return true; - -} - - -bool -p11_attr_match_value (const CK_ATTRIBUTE *attr, - const void *value, - ssize_t length) -{ - if (length < 0) - length = strlen (value); - return (attr != NULL && - attr->ulValueLen == length && - (attr->pValue == value || - (attr->pValue && value && - memcmp (attr->pValue, value, attr->ulValueLen) == 0))); -} - -bool -p11_attr_equal (const void *v1, - const void *v2) -{ - const CK_ATTRIBUTE *one = v1; - const CK_ATTRIBUTE *two = v2; - - return (one == two || - (one && two && one->type == two->type && - p11_attr_match_value (one, two->pValue, two->ulValueLen))); -} - -unsigned int -p11_attr_hash (const void *data) -{ - const CK_ATTRIBUTE *attr = data; - uint32_t hash = 0; - - if (attr != NULL) { - p11_hash_murmur3 (&hash, - &attr->type, sizeof (attr->type), - attr->pValue, (size_t)attr->ulValueLen, - NULL); - } - - return hash; -} - -static void -buffer_append_printf (p11_buffer *buffer, - const char *format, - ...) GNUC_PRINTF(2, 3); - -static void -buffer_append_printf (p11_buffer *buffer, - const char *format, - ...) -{ - char *string; - va_list va; - - va_start (va, format); - if (vasprintf (&string, format, va) < 0) - return_if_reached (); - va_end (va); - - p11_buffer_add (buffer, string, -1); - free (string); -} - -static bool -attribute_is_ulong_of_type (const CK_ATTRIBUTE *attr, - CK_ULONG type) -{ - if (attr->type != type) - return false; - if (attr->ulValueLen != sizeof (CK_ULONG)) - return false; - if (!attr->pValue) - return false; - return true; -} - -static bool -attribute_is_trust_value (const CK_ATTRIBUTE *attr) -{ - switch (attr->type) { - case CKA_TRUST_DIGITAL_SIGNATURE: - case CKA_TRUST_NON_REPUDIATION: - case CKA_TRUST_KEY_ENCIPHERMENT: - case CKA_TRUST_DATA_ENCIPHERMENT: - case CKA_TRUST_KEY_AGREEMENT: - case CKA_TRUST_KEY_CERT_SIGN: - case CKA_TRUST_CRL_SIGN: - case CKA_TRUST_SERVER_AUTH: - case CKA_TRUST_CLIENT_AUTH: - case CKA_TRUST_CODE_SIGNING: - case CKA_TRUST_EMAIL_PROTECTION: - case CKA_TRUST_IPSEC_END_SYSTEM: - case CKA_TRUST_IPSEC_TUNNEL: - case CKA_TRUST_IPSEC_USER: - case CKA_TRUST_TIME_STAMPING: - break; - default: - return false; - } - - return attribute_is_ulong_of_type (attr, attr->type); -} - -static bool -attribute_is_sensitive (const CK_ATTRIBUTE *attr, - CK_OBJECT_CLASS klass) -{ - /* - * Don't print any just attribute, since they may contain - * sensitive data - */ - - switch (attr->type) { - #define X(x) case x: return false; - X (CKA_CLASS) - X (CKA_TOKEN) - X (CKA_PRIVATE) - X (CKA_LABEL) - X (CKA_APPLICATION) - 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_BITS) - 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_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_PUBLIC_KEY_INFO) - 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) - case CKA_VALUE: - return (klass != CKO_CERTIFICATE && - klass != CKO_X_CERTIFICATE_EXTENSION); - #undef X - } - - return true; -} - -static void -format_class (p11_buffer *buffer, - CK_OBJECT_CLASS klass) -{ - const char *string = p11_constant_name (p11_constant_classes, klass); - if (string != NULL) - p11_buffer_add (buffer, string, -1); - else - buffer_append_printf (buffer, "0x%08lX", klass); -} - -static void -format_assertion_type (p11_buffer *buffer, - CK_X_ASSERTION_TYPE type) -{ - const char *string = p11_constant_name (p11_constant_asserts, type); - if (string != NULL) - p11_buffer_add (buffer, string, -1); - else - buffer_append_printf (buffer, "0x%08lX", type); -} - -static void -format_key_type (p11_buffer *buffer, - CK_KEY_TYPE type) -{ - const char *string = p11_constant_name (p11_constant_keys, type); - if (string != NULL) - p11_buffer_add (buffer, string, -1); - else - buffer_append_printf (buffer, "0x%08lX", type); -} - -static void -format_certificate_type (p11_buffer *buffer, - CK_CERTIFICATE_TYPE type) -{ - const char *string = p11_constant_name (p11_constant_certs, type); - if (string != NULL) - p11_buffer_add (buffer, string, -1); - else - buffer_append_printf (buffer, "0x%08lX", type); -} - -static void -format_trust_value (p11_buffer *buffer, - CK_TRUST trust) -{ - const char *string = p11_constant_name (p11_constant_trusts, trust); - if (string != NULL) - p11_buffer_add (buffer, string, -1); - else - buffer_append_printf (buffer, "0x%08lX", trust); -} - -static void -format_certificate_category (p11_buffer *buffer, - CK_ULONG category) -{ - const char *string = p11_constant_name (p11_constant_categories, category); - if (string != NULL) - buffer_append_printf (buffer, "%lu (%s)", category, string); - else - buffer_append_printf (buffer, "%lu", category); -} - -static void -format_attribute_type (p11_buffer *buffer, - CK_ULONG type) -{ - const char *string = p11_constant_name (p11_constant_types, type); - if (string != NULL) - p11_buffer_add (buffer, string, -1); - else - buffer_append_printf (buffer, "CKA_0x%08lX", type); -} - -static void -format_some_bytes (p11_buffer *buffer, - void *bytes, - CK_ULONG length) -{ - unsigned char ch; - const unsigned char *data = bytes; - CK_ULONG i; - - if (bytes == NULL) { - p11_buffer_add (buffer, "NULL", -1); - return; - } - - p11_buffer_add (buffer, "\"", 1); - for (i = 0; i < length && i < 128; i++) { - ch = data[i]; - if (ch == '\t') - p11_buffer_add (buffer, "\\t", -1); - else if (ch == '\n') - p11_buffer_add (buffer, "\\n", -1); - else if (ch == '\r') - p11_buffer_add (buffer, "\\r", -1); - else if (ch >= 32 && ch < 127) - p11_buffer_add (buffer, &ch, 1); - else - buffer_append_printf (buffer, "\\x%02x", ch); - } - - if (i < length) - buffer_append_printf (buffer, "..."); - p11_buffer_add (buffer, "\"", 1); -} - -void -p11_attr_format (p11_buffer *buffer, - const CK_ATTRIBUTE *attr, - CK_OBJECT_CLASS klass) -{ - p11_buffer_add (buffer, "{ ", -1); - format_attribute_type (buffer, attr->type); - p11_buffer_add (buffer, " = ", -1); - if (attr->ulValueLen == CKA_INVALID) { - buffer_append_printf (buffer, "(-1) INVALID"); - } else if (attribute_is_ulong_of_type (attr, CKA_CLASS)) { - format_class (buffer, *((CK_OBJECT_CLASS *)attr->pValue)); - } else if (attribute_is_ulong_of_type (attr, CKA_X_ASSERTION_TYPE)) { - format_assertion_type (buffer, *((CK_X_ASSERTION_TYPE *)attr->pValue)); - } else if (attribute_is_ulong_of_type (attr, CKA_CERTIFICATE_TYPE)) { - format_certificate_type (buffer, *((CK_CERTIFICATE_TYPE *)attr->pValue)); - } else if (attribute_is_ulong_of_type (attr, CKA_CERTIFICATE_CATEGORY)) { - format_certificate_category (buffer, *((CK_ULONG *)attr->pValue)); - } else if (attribute_is_ulong_of_type (attr, CKA_KEY_TYPE)) { - format_key_type (buffer, *((CK_KEY_TYPE *)attr->pValue)); - } else if (attribute_is_trust_value (attr)) { - format_trust_value (buffer, *((CK_TRUST *)attr->pValue)); - } else if (attribute_is_sensitive (attr, klass)) { - buffer_append_printf (buffer, "(%lu) NOT-PRINTED", attr->ulValueLen); - } else { - buffer_append_printf (buffer, "(%lu) ", attr->ulValueLen); - format_some_bytes (buffer, attr->pValue, attr->ulValueLen); - } - p11_buffer_add (buffer, " }", -1); -} - -void -p11_attrs_format (p11_buffer *buffer, - const CK_ATTRIBUTE *attrs, - int count) -{ - CK_BBOOL first = CK_TRUE; - CK_OBJECT_CLASS klass; - int i; - - if (count < 0) - count = p11_attrs_count (attrs); - - if (!p11_attrs_findn_ulong (attrs, CKA_CLASS, count, &klass)) - klass = CKA_INVALID; - - buffer_append_printf (buffer, "(%d) [", count); - for (i = 0; i < count; i++) { - if (first) - p11_buffer_add (buffer, " ", 1); - else - p11_buffer_add (buffer, ", ", 2); - first = CK_FALSE; - p11_attr_format (buffer, attrs + i, klass); - } - p11_buffer_add (buffer, " ]", -1); -} - -char * -p11_attrs_to_string (const CK_ATTRIBUTE *attrs, - int count) -{ - p11_buffer buffer; - if (!p11_buffer_init_null (&buffer, 128)) - return_val_if_reached (NULL); - p11_attrs_format (&buffer, attrs, count); - return p11_buffer_steal (&buffer, NULL); -} - -char * -p11_attr_to_string (const CK_ATTRIBUTE *attr, - CK_OBJECT_CLASS klass) -{ - p11_buffer buffer; - if (!p11_buffer_init_null (&buffer, 32)) - return_val_if_reached (NULL); - p11_attr_format (&buffer, attr, klass); - return p11_buffer_steal (&buffer, NULL); -} diff --git a/common/attrs.h b/common/attrs.h deleted file mode 100644 index 2780013..0000000 --- a/common/attrs.h +++ /dev/null @@ -1,136 +0,0 @@ -/* - * 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_ATTRS_H_ -#define P11_ATTRS_H_ - -#include "buffer.h" -#include "compat.h" -#include "pkcs11.h" - -#define CKA_INVALID ((CK_ULONG)-1) - -CK_ATTRIBUTE * p11_attrs_dup (const CK_ATTRIBUTE *attrs); - -CK_ATTRIBUTE * p11_attrs_build (CK_ATTRIBUTE *attrs, - ...); - -CK_ATTRIBUTE * p11_attrs_buildn (CK_ATTRIBUTE *attrs, - const CK_ATTRIBUTE *add, - CK_ULONG count); - -CK_ATTRIBUTE * p11_attrs_take (CK_ATTRIBUTE *attrs, - CK_ATTRIBUTE_TYPE type, - CK_VOID_PTR value, - CK_ULONG length); - -CK_ATTRIBUTE * p11_attrs_merge (CK_ATTRIBUTE *attrs, - CK_ATTRIBUTE *merge, - bool replace); - -void p11_attrs_purge (CK_ATTRIBUTE *attrs); - -bool p11_attrs_terminator (const CK_ATTRIBUTE *attrs); - -CK_ULONG p11_attrs_count (const CK_ATTRIBUTE *attrs); - -void p11_attrs_free (void *attrs); - -CK_ATTRIBUTE * p11_attrs_find (CK_ATTRIBUTE *attrs, - CK_ATTRIBUTE_TYPE type); - -CK_ATTRIBUTE * p11_attrs_findn (CK_ATTRIBUTE *attrs, - CK_ULONG count, - CK_ATTRIBUTE_TYPE type); - -bool p11_attrs_find_bool (const CK_ATTRIBUTE *attrs, - CK_ATTRIBUTE_TYPE type, - CK_BBOOL *value); - -bool p11_attrs_findn_bool (const CK_ATTRIBUTE *attrs, - CK_ULONG count, - CK_ATTRIBUTE_TYPE type, - CK_BBOOL *value); - -bool p11_attrs_find_ulong (const CK_ATTRIBUTE *attrs, - CK_ATTRIBUTE_TYPE type, - CK_ULONG *value); - -bool p11_attrs_findn_ulong (const CK_ATTRIBUTE *attrs, - CK_ULONG count, - CK_ATTRIBUTE_TYPE type, - CK_ULONG *value); - -void * p11_attrs_find_value (CK_ATTRIBUTE *attrs, - CK_ATTRIBUTE_TYPE type, - size_t *length); - -CK_ATTRIBUTE * p11_attrs_find_valid (CK_ATTRIBUTE *attrs, - CK_ATTRIBUTE_TYPE type); - -bool p11_attrs_remove (CK_ATTRIBUTE *attrs, - CK_ATTRIBUTE_TYPE type); - -bool p11_attrs_match (const CK_ATTRIBUTE *attrs, - const CK_ATTRIBUTE *match); - -bool p11_attrs_matchn (const CK_ATTRIBUTE *attrs, - const CK_ATTRIBUTE *match, - CK_ULONG count); - -char * p11_attrs_to_string (const CK_ATTRIBUTE *attrs, - int count); - -void p11_attrs_format (p11_buffer *buffer, - const CK_ATTRIBUTE *attrs, - int count); - -char * p11_attr_to_string (const CK_ATTRIBUTE *attr, - CK_OBJECT_CLASS klass); - -void p11_attr_format (p11_buffer *buffer, - const CK_ATTRIBUTE *attr, - CK_OBJECT_CLASS klass); - -bool p11_attr_equal (const void *one, - const void *two); - -unsigned int p11_attr_hash (const void *data); - -bool p11_attr_match_value (const CK_ATTRIBUTE *attr, - const void *value, - ssize_t length); - -#endif /* P11_ATTRS_H_ */ diff --git a/common/buffer.c b/common/buffer.c deleted file mode 100644 index f2e2cb8..0000000 --- a/common/buffer.c +++ /dev/null @@ -1,197 +0,0 @@ -/* - * Copyright (C) 2007, 2012 Stefan Walter - * Copyright (C) 2013 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 <stef@thewalter.net> - */ - -#include "config.h" - -#include "buffer.h" -#include "debug.h" - -#include <assert.h> -#include <stdint.h> -#include <stdlib.h> -#include <string.h> -#include <stdarg.h> - -static bool -buffer_realloc (p11_buffer *buffer, - size_t size) -{ - void *data; - - /* Memory owned elsewhere can't be reallocated */ - return_val_if_fail (buffer->frealloc != NULL, false); - - /* Reallocate built in buffer using allocator */ - data = (buffer->frealloc) (buffer->data, size); - if (!data && size > 0) { - p11_buffer_fail (buffer); - return_val_if_reached (false); - } - - buffer->data = data; - buffer->size = size; - return true; -} - -bool -p11_buffer_init (p11_buffer *buffer, - size_t reserve) -{ - p11_buffer_init_full (buffer, NULL, 0, 0, realloc, free); - return buffer_realloc (buffer, reserve); -} - -bool -p11_buffer_init_null (p11_buffer *buffer, - size_t reserve) -{ - p11_buffer_init_full (buffer, NULL, 0, P11_BUFFER_NULL, realloc, free); - return buffer_realloc (buffer, reserve); -} - -void -p11_buffer_init_full (p11_buffer *buffer, - void *data, - size_t len, - int flags, - void * (* frealloc) (void *, size_t), - void (* ffree) (void *)) -{ - memset (buffer, 0, sizeof (*buffer)); - - buffer->data = data; - buffer->len = len; - buffer->size = len; - buffer->flags = flags; - buffer->frealloc = frealloc; - buffer->ffree = ffree; - - return_if_fail (!(flags & P11_BUFFER_FAILED)); -} - -void -p11_buffer_uninit (p11_buffer *buffer) -{ - return_if_fail (buffer != NULL); - - if (buffer->ffree && buffer->data) - (buffer->ffree) (buffer->data); - memset (buffer, 0, sizeof (*buffer)); -} - -void * -p11_buffer_steal (p11_buffer *buffer, - size_t *length) -{ - void *data; - - return_val_if_fail (p11_buffer_ok (buffer), NULL); - - if (length) - *length = buffer->len; - data = buffer->data; - - buffer->data = NULL; - buffer->size = 0; - buffer->len = 0; - return data; -} - -bool -p11_buffer_reset (p11_buffer *buffer, - size_t reserve) -{ - buffer->flags &= ~P11_BUFFER_FAILED; - buffer->len = 0; - - if (reserve < buffer->size) - return true; - return buffer_realloc (buffer, reserve); -} - -void * -p11_buffer_append (p11_buffer *buffer, - size_t length) -{ - unsigned char *data; - size_t terminator; - size_t newlen; - size_t reserve; - - return_val_if_fail (p11_buffer_ok (buffer), NULL); - - terminator = (buffer->flags & P11_BUFFER_NULL) ? 1 : 0; - - /* Check for unlikely and unrecoverable integer overflow */ - return_val_if_fail (SIZE_MAX - (terminator + length) > buffer->len, NULL); - - reserve = terminator + length + buffer->len; - - if (reserve > buffer->size) { - - /* Calculate a new length, minimize number of buffer allocations */ - return_val_if_fail (buffer->size < SIZE_MAX / 2, NULL); - newlen = buffer->size * 2; - if (!newlen) - newlen = 16; - if (reserve > newlen) - newlen = reserve; - - if (!buffer_realloc (buffer, newlen)) - return_val_if_reached (NULL); - } - - data = buffer->data; - data += buffer->len; - buffer->len += length; - if (terminator) - data[length] = '\0'; - return data; -} - -void -p11_buffer_add (p11_buffer *buffer, - const void *data, - ssize_t length) -{ - void *at; - - if (length < 0) - length = strlen (data); - - at = p11_buffer_append (buffer, length); - return_if_fail (at != NULL); - memcpy (at, data, length); -} diff --git a/common/buffer.h b/common/buffer.h deleted file mode 100644 index 6a61083..0000000 --- a/common/buffer.h +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (C) 2007, 2012 Stefan Walter - * 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 <stef@thewalter.net> - */ - -#ifndef P11_BUFFER_H_ -#define P11_BUFFER_H_ - -#include "compat.h" - -enum { - P11_BUFFER_FAILED = 1 << 0, - P11_BUFFER_NULL = 1 << 1, -}; - -typedef struct { - void *data; - size_t len; - - int flags; - size_t size; - void * (* frealloc) (void *, size_t); - void (* ffree) (void *); -} p11_buffer; - -bool p11_buffer_init (p11_buffer *buffer, - size_t size); - -bool p11_buffer_init_null (p11_buffer *buffer, - size_t size); - -void p11_buffer_init_full (p11_buffer *buffer, - void *data, - size_t len, - int flags, - void * (* frealloc) (void *, size_t), - void (* ffree) (void *)); - -void p11_buffer_uninit (p11_buffer *buffer); - -void * p11_buffer_steal (p11_buffer *buffer, - size_t *length); - -bool p11_buffer_reset (p11_buffer *buffer, - size_t size); - -void * p11_buffer_append (p11_buffer *buffer, - size_t length); - -void p11_buffer_add (p11_buffer *buffer, - const void *data, - ssize_t length); - -#define p11_buffer_fail(buf) \ - ((buf)->flags |= P11_BUFFER_FAILED) - -#define p11_buffer_ok(buf) \ - (((buf)->flags & P11_BUFFER_FAILED) ? false : true) - -#define p11_buffer_failed(buf) \ - (((buf)->flags & P11_BUFFER_FAILED) ? true : false) - -#endif /* BUFFER_H */ diff --git a/common/compat.c b/common/compat.c deleted file mode 100644 index de5b99b..0000000 --- a/common/compat.c +++ /dev/null @@ -1,924 +0,0 @@ -/* - * 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> - */ - -#include "config.h" - -/* - * This is needed to expose pthread_mutexattr_settype and PTHREAD_MUTEX_DEFAULT - * on older pthreads implementations - */ -#define _XOPEN_SOURCE 700 - -#include "compat.h" - -#include <assert.h> -#include <dirent.h> -#include <errno.h> -#include <stdlib.h> -#include <string.h> - -/*- - * Portions of this file are covered by the following copyright: - * - * Copyright (c) 2001 Mike Barcroft <mike@FreeBSD.org> - * Copyright (c) 1990, 1993 - * Copyright (c) 1987, 1993 - * The Regents of the University of California. All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * Chris Torek. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. 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. - * 4. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. - */ - -#ifndef HAVE_GETPROGNAME - -#ifdef OS_UNIX - -#include <unistd.h> - -#if defined (HAVE_PROGRAM_INVOCATION_SHORT_NAME) && !HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME -extern char *program_invocation_short_name; -#endif - -#if defined (HAVE___PROGNAME) && !HAVE_DECL___PROGNAME -extern char *__progname; -#endif - -const char * -getprogname (void) -{ - const char *name; - -#if defined (HAVE_GETEXECNAME) - const char *p; - name = getexecname(); - p = strrchr (name ? name : "", '/'); - if (p != NULL) - name = p + 1; -#elif defined (HAVE_PROGRAM_INVOCATION_SHORT_NAME) - name = program_invocation_short_name; -#elif defined (HAVE___PROGNAME) - name = __progname; -#else - #error No way to retrieve short program name -#endif - - return name; -} - -#else /* OS_WIN32 */ - -extern char **__argv; -static char prognamebuf[256]; - -const char * -getprogname (void) -{ - const char *name; - const char *p, *p2; - size_t length; - - name = __argv[0]; - if (name == NULL) - return NULL; - - p = strrchr (name, '\\'); - p2 = strrchr (name, '/'); - if (p2 > p) - p = p2; - if (p != NULL) - name = p + 1; - - length = sizeof (prognamebuf) - 1; - strncpy (prognamebuf, name, length); - prognamebuf[length] = 0; - length = strlen (prognamebuf); - if (length > 4 && _stricmp (prognamebuf + (length - 4), ".exe") == 0) - prognamebuf[length - 4] = '\0'; - - return prognamebuf; -} - -#endif /* OS_WIN32 */ - -#endif /* HAVE_GETPROGNAME */ - -#ifdef OS_UNIX -#include <sys/stat.h> -#include <sys/mman.h> -#include <fcntl.h> -#include <unistd.h> - -void -p11_mutex_init (p11_mutex_t *mutex) -{ - pthread_mutexattr_t attr; - int ret; - - pthread_mutexattr_init (&attr); - pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_DEFAULT); - ret = pthread_mutex_init (mutex, &attr); - assert (ret == 0); - pthread_mutexattr_destroy (&attr); -} - -char * -p11_dl_error (void) -{ - const char *msg = dlerror (); - return msg ? strdup (msg) : NULL; -} - -struct _p11_mmap { - int fd; - void *data; - size_t size; -}; - -p11_mmap * -p11_mmap_open (const char *path, - struct stat *sb, - void **data, - size_t *size) -{ - struct stat stb; - p11_mmap *map; - - map = calloc (1, sizeof (p11_mmap)); - if (map == NULL) - return NULL; - - map->fd = open (path, O_RDONLY | O_CLOEXEC); - if (map->fd == -1) { - free (map); - return NULL; - } - - if (sb == NULL) { - sb = &stb; - if (fstat (map->fd, &stb) < 0) { - close (map->fd); - free (map); - return NULL; - } - } - - /* Workaround for broken ZFS on Linux */ - if (S_ISDIR (sb->st_mode)) { - errno = EISDIR; - close (map->fd); - free (map); - return NULL; - } - - if (sb->st_size == 0) { - *data = ""; - *size = 0; - return map; - } - - map->size = sb->st_size; - map->data = mmap (NULL, map->size, PROT_READ, MAP_PRIVATE, map->fd, 0); - if (map->data == MAP_FAILED) { - close (map->fd); - free (map); - return NULL; - } - - *data = map->data; - *size = map->size; - return map; -} - -void -p11_mmap_close (p11_mmap *map) -{ - if (map->size) - munmap (map->data, map->size); - close (map->fd); - free (map); -} - -#endif /* OS_UNIX */ - -#ifdef OS_WIN32 - -char * -p11_dl_error (void) -{ - DWORD code = GetLastError(); - LPVOID msg_buf; - - FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER | - FORMAT_MESSAGE_FROM_SYSTEM | - FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, code, - MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPSTR)&msg_buf, 0, NULL); - - return msg_buf; -} - -void -p11_dl_close (void *dl) -{ - FreeLibrary (dl); -} - -int -p11_thread_create (p11_thread_t *thread, - p11_thread_routine routine, - void *arg) -{ - assert (thread); - - *thread = CreateThread (NULL, 0, - (LPTHREAD_START_ROUTINE)routine, - arg, 0, NULL); - - if (*thread == NULL) - return GetLastError (); - - return 0; -} - -int -p11_thread_join (p11_thread_t thread) -{ - DWORD res; - - res = WaitForSingleObject (thread, INFINITE); - if (res == WAIT_FAILED) - return GetLastError (); - - CloseHandle (thread); - return 0; -} - -struct _p11_mmap { - HANDLE file; - HANDLE mapping; - void *data; -}; - -p11_mmap * -p11_mmap_open (const char *path, - struct stat *sb, - void **data, - size_t *size) -{ - HANDLE mapping; - LARGE_INTEGER large; - DWORD errn; - p11_mmap *map; - - map = calloc (1, sizeof (p11_mmap)); - if (map == NULL) { - errno = ENOMEM; - return NULL; - } - - map->file = CreateFile (path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, NULL); - if (map->file == INVALID_HANDLE_VALUE) { - errn = GetLastError (); - free (map); - SetLastError (errn); - if (errn == ERROR_PATH_NOT_FOUND || errn == ERROR_FILE_NOT_FOUND) - errno = ENOENT; - else if (errn == ERROR_ACCESS_DENIED) - errno = EPERM; - return NULL; - } - - if (sb == NULL) { - if (!GetFileSizeEx (map->file, &large)) { - errn = GetLastError (); - CloseHandle (map->file); - free (map); - SetLastError (errn); - if (errn == ERROR_ACCESS_DENIED) - errno = EPERM; - return NULL; - } - } else { - large.QuadPart = sb->st_size; - } - - mapping = CreateFileMapping (map->file, NULL, PAGE_READONLY, 0, 0, NULL); - if (!mapping) { - errn = GetLastError (); - CloseHandle (map->file); - free (map); - SetLastError (errn); - if (errn == ERROR_ACCESS_DENIED) - errno = EPERM; - return NULL; - } - - map->data = MapViewOfFile (mapping, FILE_MAP_READ, 0, 0, large.QuadPart); - CloseHandle (mapping); - - if (map->data == NULL) { - errn = GetLastError (); - CloseHandle (map->file); - free (map); - SetLastError (errn); - if (errn == ERROR_ACCESS_DENIED) - errno = EPERM; - return NULL; - } - - *data = map->data; - *size = large.QuadPart; - return map; -} - -void -p11_mmap_close (p11_mmap *map) -{ - UnmapViewOfFile (map->data); - CloseHandle (map->file); - free (map); -} - -#endif /* OS_WIN32 */ - -#ifndef HAVE_STRNSTR -#include <string.h> - -/* - * Find the first occurrence of find in s, where the search is limited to the - * first slen characters of s. - */ -char * -strnstr (const char *s, - const char *find, - size_t slen) -{ - char c, sc; - size_t len; - - if ((c = *find++) != '\0') { - len = strlen (find); - do { - do { - if (slen-- < 1 || (sc = *s++) == '\0') - return (NULL); - } while (sc != c); - if (len > slen) - return (NULL); - } while (strncmp(s, find, len) != 0); - s--; - } - return ((char *)s); -} - -#endif /* HAVE_STRNSTR */ - -#ifndef HAVE_MEMDUP - -void * -memdup (const void *data, - size_t length) -{ - void *dup; - - if (!data) - return NULL; - - dup = malloc (length); - if (dup != NULL) - memcpy (dup, data, length); - - return dup; -} - -#endif /* HAVE_MEMDUP */ - -/* - * WORKAROUND: So in lots of released builds of firefox a completely broken strndup() - * is present. It does not NULL terminate its string output. It is unconditionally - * defined, and overrides the libc strndup() function on platforms where it - * exists as a function. For this reason we (for now) unconditionally define - * strndup(). - */ - -#if 1 /* #ifndef HAVE_STRNDUP */ - -/* - * HAVE_STRNDUP may be undefined if strndup() isn't working. So it may be - * present, and yet strndup may still be a defined header macro. - */ -#ifdef strndup -#undef strndup -#endif - -char * -strndup (const char *data, - size_t length); - -char * -strndup (const char *data, - size_t length) -{ - char *ret; - - ret = malloc (length + 1); - if (ret != NULL) { - strncpy (ret, data, length); - ret[length] = 0; - } - - return ret; -} - -#endif /* HAVE_STRNDUP */ - -#ifndef HAVE_STRCONCAT - -#include <stdarg.h> - -char * -strconcat (const char *first, - ...) -{ - size_t length = 0; - const char *arg; - char *result, *at; - va_list va; - - va_start (va, first); - - for (arg = first; arg; arg = va_arg (va, const char*)) - length += strlen (arg); - - va_end (va); - - at = result = malloc (length + 1); - if (result == NULL) - return NULL; - - va_start (va, first); - - for (arg = first; arg; arg = va_arg (va, const char*)) { - length = strlen (arg); - memcpy (at, arg, length); - at += length; - } - - va_end (va); - - *at = 0; - return result; -} - -#endif /* HAVE_STRCONCAT */ - -#ifndef HAVE_VASPRINTF -#include <stdio.h> - -int vasprintf(char **strp, const char *fmt, va_list ap); - -int -vasprintf (char **strp, - const char *fmt, - va_list ap) -{ - char *buf = NULL; - char *nbuf; - int guess = 128; - int length = 0; - int ret; - - if (fmt == NULL) { - errno = EINVAL; - return -1; - } - - for (;;) { - nbuf = realloc (buf, guess); - if (!nbuf) { - free (buf); - return -1; - } - - buf = nbuf; - length = guess; - - ret = vsnprintf (buf, length, fmt, ap); - - if (ret < 0) - guess *= 2; - - else if (ret >= length) - guess = ret + 1; - - else - break; - } - - *strp = buf; - return ret; -} - -#endif /* HAVE_VASPRINTF */ - -#ifndef HAVE_ASPRINTF - -int asprintf(char **strp, const char *fmt, ...); - -int -asprintf (char **strp, - const char *fmt, - ...) -{ - va_list va; - int ret; - - va_start (va, fmt); - ret = vasprintf (strp, fmt, va); - va_end (va); - - return ret; -} - -#endif /* HAVE_ASPRINTF */ - -#ifndef HAVE_GMTIME_R - -struct tm * -gmtime_r (const time_t *timep, - struct tm *result) -{ -#ifdef OS_WIN32 - /* - * On win32 gmtime() returns thread local storage, so we can - * just copy it out into the buffer without worrying about races. - */ - struct tm *tg; - tg = gmtime (timep); - if (!tg) - return NULL; - memcpy (result, tg, sizeof (struct tm)); - return result; -#else - #error Need either gmtime_r() function on Unix -#endif -} - -#endif /* HAVE_GMTIME_R */ - -#if !defined(HAVE_MKDTEMP) || !defined(HAVE_MKSTEMP) -#include <sys/stat.h> -#include <fcntl.h> - -static int -_gettemp (char *path, - int *doopen, - int domkdir, - int slen) -{ - static const char padchar[] = - "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - static const int maxpathlen = 1024; - - char *start, *trv, *suffp, *carryp; - char *pad; - struct stat sbuf; - int rval; - int rnd; - char carrybuf[maxpathlen]; - - if ((doopen != NULL && domkdir) || slen < 0) { - errno = EINVAL; - return (0); - } - - for (trv = path; *trv != '\0'; ++trv) - ; - if (trv - path >= maxpathlen) { - errno = ENAMETOOLONG; - return (0); - } - trv -= slen; - suffp = trv; - --trv; - if (trv < path || NULL != strchr (suffp, '/')) { - errno = EINVAL; - return (0); - } - - /* Fill space with random characters */ - while (trv >= path && *trv == 'X') { - rnd = rand () % sizeof (padchar) - 1; - *trv-- = padchar[rnd]; - } - start = trv + 1; - - /* save first combination of random characters */ - memcpy (carrybuf, start, suffp - start); - - /* - * check the target directory. - */ - if (doopen != NULL || domkdir) { - for (; trv > path; --trv) { - if (*trv == '/') { - *trv = '\0'; - rval = stat(path, &sbuf); - *trv = '/'; - if (rval != 0) - return (0); - if (!S_ISDIR(sbuf.st_mode)) { - errno = ENOTDIR; - return (0); - } - break; - } - } - } - - for (;;) { - if (doopen) { - if ((*doopen = open (path, O_BINARY | O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC, 0600)) >= 0) - return (1); - if (errno != EEXIST) - return (0); - } else if (domkdir) { -#ifdef OS_UNIX - if (mkdir (path, 0700) == 0) -#else - if (mkdir (path) == 0) -#endif - return (1); - if (errno != EEXIST) - return (0); -#ifdef OS_UNIX - } else if (lstat (path, &sbuf)) -#else - } else if (stat (path, &sbuf)) -#endif - return (errno == ENOENT); - - /* If we have a collision, cycle through the space of filenames */ - for (trv = start, carryp = carrybuf;;) { - /* have we tried all possible permutations? */ - if (trv == suffp) - return (0); /* yes - exit with EEXIST */ - pad = strchr(padchar, *trv); - if (pad == NULL) { - /* this should never happen */ - errno = EIO; - return (0); - } - /* increment character */ - *trv = (*++pad == '\0') ? padchar[0] : *pad; - /* carry to next position? */ - if (*trv == *carryp) { - /* increment position and loop */ - ++trv; - ++carryp; - } else { - /* try with new name */ - break; - } - } - } - - /*NOTREACHED*/ -} - -#endif /* !HAVE_MKDTEMP || !HAVE_MKSTEMP */ - -#ifndef HAVE_MKSTEMP - -int -mkstemp (char *template) -{ - int fd; - - return (_gettemp (template, &fd, 0, 0) ? fd : -1); -} - -#endif /* HAVE_MKSTEMP */ - -#ifndef HAVE_MKDTEMP - -char * -mkdtemp (char *template) -{ - return (_gettemp (template, (int *)NULL, 1, 0) ? template : (char *)NULL); -} - -#endif /* HAVE_MKDTEMP */ - -#ifndef HAVE_GETAUXVAL - -unsigned long -getauxval (unsigned long type) -{ - static unsigned long secure = 0UL; - static bool check_secure_initialized = false; - - /* - * This is the only one our stand-in impl supports and is - * also the only type we define in compat.h header - */ - assert (type == AT_SECURE); - - if (!check_secure_initialized) { -#if defined(HAVE___LIBC_ENABLE_SECURE) - extern int __libc_enable_secure; - secure = __libc_enable_secure; - -#elif defined(HAVE_ISSETUGID) - secure = issetugid (); - -#elif defined(OS_UNIX) - uid_t ruid, euid, suid; /* Real, effective and saved user ID's */ - gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */ - -#ifdef HAVE_GETRESUID - if (getresuid (&ruid, &euid, &suid) != 0 || - getresgid (&rgid, &egid, &sgid) != 0) -#endif /* HAVE_GETRESUID */ - { - suid = ruid = getuid (); - sgid = rgid = getgid (); - euid = geteuid (); - egid = getegid (); - } - - secure = (ruid != euid || ruid != suid || - rgid != egid || rgid != sgid); -#endif /* OS_UNIX */ - check_secure_initialized = true; - } - - return secure; -} - -#endif /* HAVE_GETAUXVAL */ - -char * -secure_getenv (const char *name) -{ - if (getauxval (AT_SECURE)) - return NULL; - return getenv (name); -} - -#ifndef HAVE_STRERROR_R - -int -strerror_r (int errnum, - char *buf, - size_t buflen) -{ -#ifdef OS_WIN32 -#if _WIN32_WINNT < 0x502 /* WinXP or older */ - int n = sys_nerr; - const char *p; - if (errnum < 0 || errnum >= n) - p = sys_errlist[n]; - else - p = sys_errlist[errnum]; - if (buf == NULL || buflen == 0) - return EINVAL; - strncpy(buf, p, buflen); - buf[buflen-1] = 0; - return 0; -#else /* Server 2003 or newer */ - return strerror_s (buf, buflen, errnum); -#endif /*_WIN32_WINNT*/ - -#else - #error no strerror_r implementation -#endif -} - -#endif /* HAVE_STRERROR_R */ - -#ifdef OS_UNIX - -#include <unistd.h> - -#ifndef HAVE_FDWALK - -#ifdef HAVE_SYS_RESOURCE_H -#include <sys/resource.h> -#endif - -int -fdwalk (int (* cb) (void *data, int fd), - void *data) -{ - struct dirent *de; - char *end; - DIR *dir; - int open_max; - long num; - int res = 0; - int fd; - -#ifdef HAVE_SYS_RESOURCE_H - struct rlimit rl; -#endif - - dir = opendir ("/proc/self/fd"); - if (dir != NULL) { - while ((de = readdir (dir)) != NULL) { - end = NULL; - num = (int) strtol (de->d_name, &end, 10); - - /* didn't parse or is the opendir() fd */ - if (!end || *end != '\0' || - (int)num == dirfd (dir)) - continue; - - fd = num; - - /* call the callback */ - res = cb (data, fd); - if (res != 0) - break; - } - - closedir (dir); - return res; - } - - /* No /proc, brute force */ -#ifdef HAVE_SYS_RESOURCE_H - if (getrlimit (RLIMIT_NOFILE, &rl) == 0 && rl.rlim_max != RLIM_INFINITY) - open_max = rl.rlim_max; - else -#endif - open_max = sysconf (_SC_OPEN_MAX); - - for (fd = 0; fd < open_max; fd++) { - res = cb (data, fd); - if (res != 0) - break; - } - - return res; -} - -#endif /* HAVE_FDWALK */ - -#endif /* OS_UNIX */ diff --git a/common/compat.h b/common/compat.h deleted file mode 100644 index acbccf9..0000000 --- a/common/compat.h +++ /dev/null @@ -1,341 +0,0 @@ -/* - * 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 __COMPAT_H__ -#define __COMPAT_H__ - -#include "config.h" - -#include <sys/types.h> -#include <sys/stat.h> - -#ifdef _GNU_SOURCE -#error Make the crap stop. _GNU_SOURCE is completely unportable and breaks all sorts of behavior -#endif - -#if !defined(__cplusplus) && (__GNUC__ > 2) -#define GNUC_PRINTF(x, y) __attribute__((__format__(__printf__, x, y))) -#else -#define GNUC_PRINTF(x, y) -#endif - -#if __GNUC__ >= 4 -#define GNUC_NULL_TERMINATED __attribute__((__sentinel__)) -#else -#define GNUC_NULL_TERMINATED -#endif - -/* For detecting clang features */ -#ifndef __has_feature -#define __has_feature(x) 0 -#endif - -#ifndef CLANG_ANALYZER_NORETURN -#if __has_feature(attribute_analyzer_noreturn) -#define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn)) -#else -#define CLANG_ANALYZER_NORETURN -#endif -#endif - -#ifndef O_BINARY -#define O_BINARY 0 -#endif - -#ifndef O_CLOEXEC -#define O_CLOEXEC 0 -#endif - -#ifndef HAVE_GETPROGNAME -const char * getprogname (void); -#endif - -#ifndef HAVE_MKSTEMP - -int mkstemp (char *template); - -#endif /* HAVE_MKSTEMP */ - -#ifndef HAVE_MKDTEMP - -char * mkdtemp (char *template); - -#endif /* HAVE_MKDTEMP */ - -char * strdup_path_mangle (const char *template); - -/* ----------------------------------------------------------------------------- - * WIN32 - */ - -#ifdef OS_WIN32 - -#ifndef _WIN32_WINNT -#define _WIN32_WINNT 0x500 -#endif - -#ifndef _WIN32_IE -#define _WIN32_IE 0x500 -#endif - -#define WIN32_LEAN_AND_MEAN 1 -#include <windows.h> - -#include <io.h> - -/* Oh ... my ... god */ -#undef CreateMutex - -typedef CRITICAL_SECTION p11_mutex_t; - -typedef HANDLE p11_thread_t; - -typedef DWORD p11_thread_id_t; - -#define p11_mutex_init(m) \ - (InitializeCriticalSection (m)) -#define p11_mutex_lock(m) \ - (EnterCriticalSection (m)) -#define p11_mutex_unlock(m) \ - (LeaveCriticalSection (m)) -#define p11_mutex_uninit(m) \ - (DeleteCriticalSection (m)) - -typedef void * (*p11_thread_routine) (void *arg); - -int p11_thread_create (p11_thread_t *thread, p11_thread_routine, void *arg); - -int p11_thread_join (p11_thread_t thread); - -/* Returns a thread_id_t */ -#define p11_thread_id_self() \ - (GetCurrentThreadId ()) - -typedef HMODULE dl_module_t; - -#define p11_dl_open(f) \ - (LoadLibrary (f)) -#define p11_dl_symbol(d, s) \ - ((void *)GetProcAddress ((d), (s))) - -char * p11_dl_error (void); - -void p11_dl_close (void * dl); - -#define p11_sleep_ms(ms) \ - (Sleep (ms)) - -typedef struct _p11_mmap p11_mmap; - -p11_mmap * p11_mmap_open (const char *path, - struct stat *sb, - void **data, - size_t *size); - -void p11_mmap_close (p11_mmap *map); - -#ifndef HAVE_SETENV -#define setenv(n, v, z) _putenv_s(n, v) -#endif /* HAVE_SETENV */ - -#endif /* OS_WIN32 */ - -/* ---------------------------------------------------------------------------- - * UNIX - */ - -#ifdef OS_UNIX - -#include <pthread.h> -#include <dlfcn.h> -#include <time.h> -#include <unistd.h> - -typedef pthread_mutex_t p11_mutex_t; - -void p11_mutex_init (p11_mutex_t *mutex); - -#define p11_mutex_lock(m) \ - (pthread_mutex_lock (m)) -#define p11_mutex_unlock(m) \ - (pthread_mutex_unlock (m)) -#define p11_mutex_uninit(m) \ - (pthread_mutex_destroy(m)) - -typedef pthread_t p11_thread_t; - -typedef pthread_t p11_thread_id_t; - -typedef void * (*p11_thread_routine) (void *arg); - -#define p11_thread_create(t, r, a) \ - (pthread_create ((t), NULL, (r), (a))) -#define p11_thread_join(t) \ - (pthread_join ((t), NULL)) -#define p11_thread_id_self(m) \ - (pthread_self ()) - -typedef void * dl_module_t; - -#define p11_dl_open(f) \ - (dlopen ((f), RTLD_LOCAL | RTLD_NOW)) -#define p11_dl_close \ - dlclose -#define p11_dl_symbol(d, s) \ - (dlsym ((d), (s))) - -char * p11_dl_error (void); - -#define p11_sleep_ms(ms) \ - do { int _ms = (ms); \ - struct timespec _ts = { _ms / 1000, (_ms % 1000) * 1000 * 1000 }; \ - nanosleep (&_ts, NULL); \ - } while(0) - -typedef struct _p11_mmap p11_mmap; - -p11_mmap * p11_mmap_open (const char *path, - struct stat *sb, - void **data, - size_t *size); - -void p11_mmap_close (p11_mmap *map); - -#endif /* OS_UNIX */ - -/* ---------------------------------------------------------------------------- - * MORE COMPAT - */ - -#ifdef HAVE_ERRNO_H -#include <errno.h> -#endif /* HAVE_ERRNO_H */ - -#ifndef HAVE_STRNSTR - -char * strnstr (const char *s, - const char *find, - size_t slen); - -#endif /* HAVE_STRNSTR */ - -#ifndef HAVE_MEMDUP - -void * memdup (const void *data, - size_t length); - -#endif /* HAVE_MEMDUP */ - -#ifndef HAVE_STRNDUP - -char * strndup (const char *data, - size_t length); - -#endif /* HAVE_STRDUP */ - -#ifdef HAVE_STDBOOL_H -#include <stdbool.h> -#else -typedef enum { false, true } bool; -#endif - -#ifndef HAVE_STRCONCAT - -char * strconcat (const char *first, - ...) GNUC_NULL_TERMINATED; - -#endif /* HAVE_STRCONCAT */ - -#if defined HAVE_DECL_ASPRINTF && !HAVE_DECL_ASPRINTF - -int asprintf (char **strp, - const char *fmt, - ...); - -#endif /* HAVE_ASPRINTF */ - -#if defined HAVE_DECL_VASPRINTF && !HAVE_DECL_VASPRINTF -#include <stdarg.h> - -int vasprintf (char **strp, - const char *fmt, - va_list ap); - -#endif /* HAVE_DECL_VASPRINTF */ - -#ifndef HAVE_GMTIME_R -#include <time.h> - -struct tm * gmtime_r (const time_t *timep, - struct tm *result); - -#endif /* HAVE_GMTIME_R */ - -#ifndef HAVE_TIMEGM -#include <time.h> - -time_t timegm (struct tm *tm); - -#endif /* HAVE_TIMEGM */ - -#ifdef HAVE_GETAUXVAL - -#include <sys/auxv.h> - -#else /* !HAVE_GETAUXVAL */ - -unsigned long getauxval (unsigned long type); - -#define AT_SECURE 23 - -#endif /* !HAVE_GETAUXVAL */ - -char * secure_getenv (const char *name); - -#ifndef HAVE_STRERROR_R - -int strerror_r (int errnum, - char *buf, - size_t buflen); - -#endif /* HAVE_STRERROR_R */ - -#ifndef HAVE_FDWALK - -int fdwalk (int (* cb) (void *data, int fd), - void *data); - -#endif - -#endif /* __COMPAT_H__ */ diff --git a/common/constants.c b/common/constants.c deleted file mode 100644 index f4aa66b..0000000 --- a/common/constants.c +++ /dev/null @@ -1,708 +0,0 @@ -/* - * 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 "pkcs11i.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 } }, -#define CT2(x, n, n2) { x, #x, { n, n2 } }, - -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") - CT2 (CKA_PUBLIC_KEY_INFO, "public-key-info", "x-public-key-info") - 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, "value-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, "x-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 }, -}; - -const p11_constant p11_constant_users[] = { - CT (CKU_SO, NULL) - CT (CKU_USER, NULL) - CT (CKU_CONTEXT_SPECIFIC, NULL) - { CKA_INVALID }, -}; - -const p11_constant p11_constant_states[] = { - CT (CKS_RO_PUBLIC_SESSION, NULL) - CT (CKS_RO_USER_FUNCTIONS, NULL) - CT (CKS_RW_PUBLIC_SESSION, NULL) - CT (CKS_RW_USER_FUNCTIONS, NULL) - CT (CKS_RW_SO_FUNCTIONS, NULL) - { CKA_INVALID }, -}; - -const p11_constant p11_constant_returns[] = { - CT (CKR_OK, NULL) - CT (CKR_CANCEL, NULL) - CT (CKR_HOST_MEMORY, NULL) - CT (CKR_SLOT_ID_INVALID, NULL) - CT (CKR_GENERAL_ERROR, NULL) - CT (CKR_FUNCTION_FAILED, NULL) - CT (CKR_ARGUMENTS_BAD, NULL) - CT (CKR_NO_EVENT, NULL) - CT (CKR_NEED_TO_CREATE_THREADS, NULL) - CT (CKR_CANT_LOCK, NULL) - CT (CKR_ATTRIBUTE_READ_ONLY, NULL) - CT (CKR_ATTRIBUTE_SENSITIVE, NULL) - CT (CKR_ATTRIBUTE_TYPE_INVALID, NULL) - CT (CKR_ATTRIBUTE_VALUE_INVALID, NULL) - CT (CKR_DATA_INVALID, NULL) - CT (CKR_DATA_LEN_RANGE, NULL) - CT (CKR_DEVICE_ERROR, NULL) - CT (CKR_DEVICE_MEMORY, NULL) - CT (CKR_DEVICE_REMOVED, NULL) - CT (CKR_ENCRYPTED_DATA_INVALID, NULL) - CT (CKR_ENCRYPTED_DATA_LEN_RANGE, NULL) - CT (CKR_FUNCTION_CANCELED, NULL) - CT (CKR_FUNCTION_NOT_PARALLEL, NULL) - CT (CKR_FUNCTION_NOT_SUPPORTED, NULL) - CT (CKR_KEY_HANDLE_INVALID, NULL) - CT (CKR_KEY_SIZE_RANGE, NULL) - CT (CKR_KEY_TYPE_INCONSISTENT, NULL) - CT (CKR_KEY_NOT_NEEDED, NULL) - CT (CKR_KEY_CHANGED, NULL) - CT (CKR_KEY_NEEDED, NULL) - CT (CKR_KEY_INDIGESTIBLE, NULL) - CT (CKR_KEY_FUNCTION_NOT_PERMITTED, NULL) - CT (CKR_KEY_NOT_WRAPPABLE, NULL) - CT (CKR_KEY_UNEXTRACTABLE, NULL) - CT (CKR_MECHANISM_INVALID, NULL) - CT (CKR_MECHANISM_PARAM_INVALID, NULL) - CT (CKR_OBJECT_HANDLE_INVALID, NULL) - CT (CKR_OPERATION_ACTIVE, NULL) - CT (CKR_OPERATION_NOT_INITIALIZED, NULL) - CT (CKR_PIN_INCORRECT, NULL) - CT (CKR_PIN_INVALID, NULL) - CT (CKR_PIN_LEN_RANGE, NULL) - CT (CKR_PIN_EXPIRED, NULL) - CT (CKR_PIN_LOCKED, NULL) - CT (CKR_SESSION_CLOSED, NULL) - CT (CKR_SESSION_COUNT, NULL) - CT (CKR_SESSION_HANDLE_INVALID, NULL) - CT (CKR_SESSION_PARALLEL_NOT_SUPPORTED, NULL) - CT (CKR_SESSION_READ_ONLY, NULL) - CT (CKR_SESSION_EXISTS, NULL) - CT (CKR_SESSION_READ_ONLY_EXISTS, NULL) - CT (CKR_SESSION_READ_WRITE_SO_EXISTS, NULL) - CT (CKR_SIGNATURE_INVALID, NULL) - CT (CKR_SIGNATURE_LEN_RANGE, NULL) - CT (CKR_TEMPLATE_INCOMPLETE, NULL) - CT (CKR_TEMPLATE_INCONSISTENT, NULL) - CT (CKR_TOKEN_NOT_PRESENT, NULL) - CT (CKR_TOKEN_NOT_RECOGNIZED, NULL) - CT (CKR_TOKEN_WRITE_PROTECTED, NULL) - CT (CKR_UNWRAPPING_KEY_HANDLE_INVALID, NULL) - CT (CKR_UNWRAPPING_KEY_SIZE_RANGE, NULL) - CT (CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT, NULL) - CT (CKR_USER_ALREADY_LOGGED_IN, NULL) - CT (CKR_USER_NOT_LOGGED_IN, NULL) - CT (CKR_USER_PIN_NOT_INITIALIZED, NULL) - CT (CKR_USER_TYPE_INVALID, NULL) - CT (CKR_USER_ANOTHER_ALREADY_LOGGED_IN, NULL) - CT (CKR_USER_TOO_MANY_TYPES, NULL) - CT (CKR_WRAPPED_KEY_INVALID, NULL) - CT (CKR_WRAPPED_KEY_LEN_RANGE, NULL) - CT (CKR_WRAPPING_KEY_HANDLE_INVALID, NULL) - CT (CKR_WRAPPING_KEY_SIZE_RANGE, NULL) - CT (CKR_WRAPPING_KEY_TYPE_INCONSISTENT, NULL) - CT (CKR_RANDOM_SEED_NOT_SUPPORTED, NULL) - CT (CKR_RANDOM_NO_RNG, NULL) - CT (CKR_DOMAIN_PARAMS_INVALID, NULL) - CT (CKR_BUFFER_TOO_SMALL, NULL) - CT (CKR_SAVED_STATE_INVALID, NULL) - CT (CKR_INFORMATION_SENSITIVE, NULL) - CT (CKR_STATE_UNSAVEABLE, NULL) - CT (CKR_CRYPTOKI_NOT_INITIALIZED, NULL) - CT (CKR_CRYPTOKI_ALREADY_INITIALIZED, NULL) - CT (CKR_MUTEX_BAD, NULL) - CT (CKR_MUTEX_NOT_LOCKED, NULL) - CT (CKR_FUNCTION_REJECTED, NULL) - { CKA_INVALID }, -}; - -const p11_constant p11_constant_mechanisms[] = { - CT (CKM_RSA_PKCS_KEY_PAIR_GEN, "rsa-pkcs-key-pair-gen") - CT (CKM_RSA_PKCS, "rsa-pkcs") - CT (CKM_RSA_9796, "rsa-9796") - CT (CKM_RSA_X_509, "rsa-x-509") - CT (CKM_MD2_RSA_PKCS, "md2-rsa-pkcs") - CT (CKM_MD5_RSA_PKCS, "md5-rsa-pkcs") - CT (CKM_SHA1_RSA_PKCS, "sha1-rsa-pkcs") - CT (CKM_RIPEMD128_RSA_PKCS, "ripemd128-rsa-pkcs") - CT (CKM_RIPEMD160_RSA_PKCS, "ripemd160-rsa-pkcs") - CT (CKM_RSA_PKCS_OAEP, "rsa-pkcs-oaep") - CT (CKM_RSA_X9_31_KEY_PAIR_GEN, "rsa-x9-31-key-pair-gen") - CT (CKM_RSA_X9_31, "rsa-x9-31") - CT (CKM_SHA1_RSA_X9_31, "sha1-rsa-x9-31") - CT (CKM_RSA_PKCS_PSS, "rsa-pkcs-pss") - CT (CKM_SHA1_RSA_PKCS_PSS, "sha1-rsa-pkcs-pss") - CT (CKM_DSA_KEY_PAIR_GEN, "dsa-key-pair-gen") - CT (CKM_DSA, NULL) /* "dsa" */ - CT (CKM_DSA_SHA1, "dsa-sha1") - CT (CKM_DH_PKCS_KEY_PAIR_GEN, "dh-pkcs-key-pair-gen") - CT (CKM_DH_PKCS_DERIVE, "dh-pkcs-derive") - CT (CKM_X9_42_DH_KEY_PAIR_GEN, "x9-42-dh-key-pair-gen") - CT (CKM_X9_42_DH_DERIVE, "x9-42-dh-derive") - CT (CKM_X9_42_DH_HYBRID_DERIVE, "x9-42-dh-hybrid-derive") - CT (CKM_X9_42_MQV_DERIVE, "x9-42-mqv-derive") - CT (CKM_SHA256_RSA_PKCS, "sha256-rsa-pkcs") - CT (CKM_SHA384_RSA_PKCS, "sha384-rsa-pkcs") - CT (CKM_SHA512_RSA_PKCS, "sha512-rsa-pkcs") - CT (CKM_SHA256_RSA_PKCS_PSS, "sha256-rsa-pkcs-pss") - CT (CKM_SHA384_RSA_PKCS_PSS, "sha384-rsa-pkcs-pss") - CT (CKM_SHA512_RSA_PKCS_PSS, "sha512-rsa-pkcs-pss") - CT (CKM_RC2_KEY_GEN, "rc2-key-gen") - CT (CKM_RC2_ECB, "rc2-ecb") - CT (CKM_RC2_CBC, "rc2-cbc") - CT (CKM_RC2_MAC, "rc2-mac") - CT (CKM_RC2_MAC_GENERAL, "rc2-mac-general") - CT (CKM_RC2_CBC_PAD, "rc2-cbc-pad") - CT (CKM_RC4_KEY_GEN, "rc4-key-gen") - CT (CKM_RC4, NULL) /* "rc4" */ - CT (CKM_DES_KEY_GEN, "des-key-gen") - CT (CKM_DES_ECB, "des-ecb") - CT (CKM_DES_CBC, "des-cbc") - CT (CKM_DES_MAC, "des-mac") - CT (CKM_DES_MAC_GENERAL, "des-mac-general") - CT (CKM_DES_CBC_PAD, "des-cbc-pad") - CT (CKM_DES2_KEY_GEN, "des2-key-gen") - CT (CKM_DES3_KEY_GEN, "des3-key-gen") - CT (CKM_DES3_ECB, "des3-ecb") - CT (CKM_DES3_CBC, "des3-cbc") - CT (CKM_DES3_MAC, "des3-mac") - CT (CKM_DES3_MAC_GENERAL, "des3-mac-general") - CT (CKM_DES3_CBC_PAD, "des3-cbc-pad") - CT (CKM_CDMF_KEY_GEN, "cdmf-key-gen") - CT (CKM_CDMF_ECB, "cdmf-ecb") - CT (CKM_CDMF_CBC, "cdmf-cbc") - CT (CKM_CDMF_MAC, "cdmf-mac") - CT (CKM_CDMF_MAC_GENERAL, "cdmf-mac-general") - CT (CKM_CDMF_CBC_PAD, "cdmf-cbc-pad") - CT (CKM_DES_OFB64, "des-ofb64") - CT (CKM_DES_OFB8, "des-ofb8") - CT (CKM_DES_CFB64, "des-cfb64") - CT (CKM_DES_CFB8, "des-cfb8") - CT (CKM_MD2, "md2") - CT (CKM_MD2_HMAC, "md2-hmac") - CT (CKM_MD2_HMAC_GENERAL, "md2-hmac-general") - CT (CKM_MD5, "md5") - CT (CKM_MD5_HMAC, "md5-hmac") - CT (CKM_MD5_HMAC_GENERAL, "md5-hmac-general") - CT (CKM_SHA_1, "sha-1") - CT (CKM_SHA_1_HMAC, "sha-1-hmac") - CT (CKM_SHA_1_HMAC_GENERAL, "sha-1-hmac-general") - CT (CKM_RIPEMD128, "ripemd128") - CT (CKM_RIPEMD128_HMAC, "ripemd128-hmac") - CT (CKM_RIPEMD128_HMAC_GENERAL, "ripemd128-hmac-general") - CT (CKM_RIPEMD160, "ripemd160") - CT (CKM_RIPEMD160_HMAC, "ripemd160-hmac") - CT (CKM_RIPEMD160_HMAC_GENERAL, "ripemd160-hmac-general") - CT (CKM_SHA256, "sha256") - CT (CKM_SHA256_HMAC, "sha256-hmac") - CT (CKM_SHA256_HMAC_GENERAL, "sha256-hmac-general") - CT (CKM_SHA384, "sha384") - CT (CKM_SHA384_HMAC, "sha384-hmac") - CT (CKM_SHA384_HMAC_GENERAL, "sha384-hmac-general") - CT (CKM_SHA512, "sha512") - CT (CKM_SHA512_HMAC, "sha512-hmac") - CT (CKM_SHA512_HMAC_GENERAL, "sha512-hmac-general") - CT (CKM_CAST_KEY_GEN, "cast-key-gen") - CT (CKM_CAST_ECB, "cast-ecb") - CT (CKM_CAST_CBC, "cast-cbc") - CT (CKM_CAST_MAC, "cast-mac") - CT (CKM_CAST_MAC_GENERAL, "cast-mac-general") - CT (CKM_CAST_CBC_PAD, "cast-cbc-pad") - CT (CKM_CAST3_KEY_GEN, "cast3-key-gen") - CT (CKM_CAST3_ECB, "cast3-ecb") - CT (CKM_CAST3_CBC, "cast3-cbc") - CT (CKM_CAST3_MAC, "cast3-mac") - CT (CKM_CAST3_MAC_GENERAL, "cast3-mac-general") - CT (CKM_CAST3_CBC_PAD, "cast3-cbc-pad") - CT (CKM_CAST5_KEY_GEN, "cast5-key-gen") - /* CT (CKM_CAST128_KEY_GEN) */ - CT (CKM_CAST5_ECB, "cast5-ecb") - /* CT (CKM_CAST128_ECB) */ - CT (CKM_CAST5_CBC, "cast5-cbc") - /* CT (CKM_CAST128_CBC) */ - CT (CKM_CAST5_MAC, "cast5-mac") - /* CT (CKM_CAST128_MAC) */ - CT (CKM_CAST5_MAC_GENERAL, "cast5-mac-general") - /* CT (CKM_CAST128_MAC_GENERAL) */ - CT (CKM_CAST5_CBC_PAD, "cast5-cbc-pad") - /* CT (CKM_CAST128_CBC_PAD) */ - CT (CKM_RC5_KEY_GEN, "rc5-key-gen") - CT (CKM_RC5_ECB, "rc5-ecb") - CT (CKM_RC5_CBC, "rc5-cbc") - CT (CKM_RC5_MAC, "rc5-mac") - CT (CKM_RC5_MAC_GENERAL, "rc5-mac-general") - CT (CKM_RC5_CBC_PAD, "rc5-cbc-pad") - CT (CKM_IDEA_KEY_GEN, "idea-key-gen") - CT (CKM_IDEA_ECB, "idea-ecb") - CT (CKM_IDEA_CBC, "idea-cbc") - CT (CKM_IDEA_MAC, "idea-mac") - CT (CKM_IDEA_MAC_GENERAL, "idea-mac-general") - CT (CKM_IDEA_CBC_PAD, "idea-cbc-pad") - CT (CKM_GENERIC_SECRET_KEY_GEN, "generic-secret-key-gen") - CT (CKM_CONCATENATE_BASE_AND_KEY, "concatenate-base-and-key") - CT (CKM_CONCATENATE_BASE_AND_DATA, "concatenate-base-and-data") - CT (CKM_CONCATENATE_DATA_AND_BASE, "concatenate-data-and-base") - CT (CKM_XOR_BASE_AND_DATA, "xor-base-and-data") - CT (CKM_EXTRACT_KEY_FROM_KEY, "extract-key-from-key") - CT (CKM_SSL3_PRE_MASTER_KEY_GEN, "ssl3-pre-master-key-gen") - CT (CKM_SSL3_MASTER_KEY_DERIVE, "ssl3-master-key-derive") - CT (CKM_SSL3_KEY_AND_MAC_DERIVE, "ssl3-key-and-mac-derive") - CT (CKM_SSL3_MASTER_KEY_DERIVE_DH, "ssl3-master-key-derive-dh") - CT (CKM_TLS_PRE_MASTER_KEY_GEN, "tls-pre-master-key-gen") - CT (CKM_TLS_MASTER_KEY_DERIVE, "tls-master-key-derive") - CT (CKM_TLS_KEY_AND_MAC_DERIVE, "tls-key-and-mac-derive") - CT (CKM_TLS_MASTER_KEY_DERIVE_DH, "tls-master-key-derive-dh") - /* CT (CKM_TLS_PRF) */ - CT (CKM_SSL3_MD5_MAC, "ssl3-md5-mac") - CT (CKM_SSL3_SHA1_MAC, "ssl3-sha1-mac") - CT (CKM_MD5_KEY_DERIVATION, "md5-key-derivation") - CT (CKM_MD2_KEY_DERIVATION, "md2-key-derivation") - CT (CKM_SHA1_KEY_DERIVATION, "sha1-key-derivation") - CT (CKM_SHA256_KEY_DERIVATION, "sha256-key-derivation") - CT (CKM_SHA384_KEY_DERIVATION, "sha384-key-derivation") - CT (CKM_SHA512_KEY_DERIVATION, "sha512-key-derivation") - CT (CKM_PBE_MD2_DES_CBC, "pbe-md2-des-cbc") - CT (CKM_PBE_MD5_DES_CBC, "pbe-md5-des-cbc") - CT (CKM_PBE_MD5_CAST_CBC, "pbe-md5-cast-cbc") - CT (CKM_PBE_MD5_CAST3_CBC, "pbe-md5-cast3-cbc") - CT (CKM_PBE_MD5_CAST5_CBC, "pbe-md5-cast5-cbc") - /* CT (CKM_PBE_MD5_CAST128_CBC) */ - CT (CKM_PBE_SHA1_CAST5_CBC, "pbe-sha1-cast5-cbc") - /* CT (CKM_PBE_SHA1_CAST128_CBC) */ - CT (CKM_PBE_SHA1_RC4_128, "pbe-sha1-rc4-128") - CT (CKM_PBE_SHA1_RC4_40, "pbe-sha1-rc4-40") - CT (CKM_PBE_SHA1_DES3_EDE_CBC, "pbe-sha1-des3-ede-cbc") - CT (CKM_PBE_SHA1_DES2_EDE_CBC, "pbe-sha1-des2-ede-cbc") - CT (CKM_PBE_SHA1_RC2_128_CBC, "pbe-sha1-rc2-128-cbc") - CT (CKM_PBE_SHA1_RC2_40_CBC, "pbe-sha1-rc2-40-cbc") - CT (CKM_PKCS5_PBKD2, "pkcs5-pbkd2") - CT (CKM_PBA_SHA1_WITH_SHA1_HMAC, "pba-sha1-with-sha1-hmac") - CT (CKM_WTLS_PRE_MASTER_KEY_GEN, "wtls-pre-master-key-gen") - CT (CKM_WTLS_MASTER_KEY_DERIVE, "wtls-master-key-derive") - CT (CKM_WTLS_MASTER_KEY_DERIVE_DH_ECC, "wtls-master-key-derive-dh-ecc") - CT (CKM_WTLS_PRF, "wtls-prf") - CT (CKM_WTLS_SERVER_KEY_AND_MAC_DERIVE, "wtls-server-key-and-mac-derive") - CT (CKM_WTLS_CLIENT_KEY_AND_MAC_DERIVE, "wtls-client-key-and-mac-derive") - CT (CKM_KEY_WRAP_LYNKS, "key-wrap-lynks") - CT (CKM_KEY_WRAP_SET_OAEP, "key-wrap-set-oaep") - CT (CKM_CMS_SIG, "cms-sig") - CT (CKM_SKIPJACK_KEY_GEN, "skipjack-key-gen") - CT (CKM_SKIPJACK_ECB64, "skipjack-ecb64") - CT (CKM_SKIPJACK_CBC64, "skipjack-cbc64") - CT (CKM_SKIPJACK_OFB64, "skipjack-ofb64") - CT (CKM_SKIPJACK_CFB64, "skipjack-cfb64") - CT (CKM_SKIPJACK_CFB32, "skipjack-cfb32") - CT (CKM_SKIPJACK_CFB16, "skipjack-cfb16") - CT (CKM_SKIPJACK_CFB8, "skipjack-cfb8") - CT (CKM_SKIPJACK_WRAP, "skipjack-wrap") - CT (CKM_SKIPJACK_PRIVATE_WRAP, "skipjack-private-wrap") - CT (CKM_SKIPJACK_RELAYX, "skipjack-relayx") - CT (CKM_KEA_KEY_PAIR_GEN, "kea-key-pair-gen") - CT (CKM_KEA_KEY_DERIVE, "kea-key-derive") - CT (CKM_FORTEZZA_TIMESTAMP, "fortezza-timestamp") - CT (CKM_BATON_KEY_GEN, "baton-key-gen") - CT (CKM_BATON_ECB128, "baton-ecb128") - CT (CKM_BATON_ECB96, "baton-ecb96") - CT (CKM_BATON_CBC128, "baton-cbc128") - CT (CKM_BATON_COUNTER, "baton-counter") - CT (CKM_BATON_SHUFFLE, "baton-shuffle") - CT (CKM_BATON_WRAP, "baton-wrap") - CT (CKM_ECDSA_KEY_PAIR_GEN, "ecdsa-key-pair-gen") - /* CT (CKM_EC_KEY_PAIR_GEN) */ - CT (CKM_ECDSA, "ecdsa") - CT (CKM_ECDSA_SHA1, "ecdsa-sha1") - CT (CKM_ECDH1_DERIVE, "ecdh1-derive") - CT (CKM_ECDH1_COFACTOR_DERIVE, "ecdh1-cofactor-derive") - CT (CKM_ECMQV_DERIVE, "ecmqv-derive") - CT (CKM_JUNIPER_KEY_GEN, "juniper-key-gen") - CT (CKM_JUNIPER_ECB128, "juniper-ecb128") - CT (CKM_JUNIPER_CBC128, "juniper-cbc128") - CT (CKM_JUNIPER_COUNTER, "juniper-counter") - CT (CKM_JUNIPER_SHUFFLE, "juniper-shuffle") - CT (CKM_JUNIPER_WRAP, "juniper-wrap") - CT (CKM_FASTHASH, "fasthash") - CT (CKM_AES_KEY_GEN, "aes-key-gen") - CT (CKM_AES_ECB, "aes-ecb") - CT (CKM_AES_CBC, "aes-cbc") - CT (CKM_AES_MAC, "aes-mac") - CT (CKM_AES_MAC_GENERAL, "aes-mac-general") - CT (CKM_AES_CBC_PAD, "aes-cbc-pad") - CT (CKM_BLOWFISH_KEY_GEN, "blowfish-key-gen") - CT (CKM_BLOWFISH_CBC, "blowfish-cbc") - CT (CKM_TWOFISH_KEY_GEN, "twofish-key-gen") - CT (CKM_TWOFISH_CBC, "twofish-cbc") - CT (CKM_DES_ECB_ENCRYPT_DATA, "des-ecb-encrypt-data") - CT (CKM_DES_CBC_ENCRYPT_DATA, "des-cbc-encrypt-data") - CT (CKM_DES3_ECB_ENCRYPT_DATA, "des3-ecb-encrypt-data") - CT (CKM_DES3_CBC_ENCRYPT_DATA, "des3-cbc-encrypt-data") - CT (CKM_AES_ECB_ENCRYPT_DATA, "aes-ecb-encrypt-data") - CT (CKM_AES_CBC_ENCRYPT_DATA, "aes-cbc-encrypt-data") - CT (CKM_DSA_PARAMETER_GEN, "dsa-parameter-gen") - CT (CKM_DH_PKCS_PARAMETER_GEN, "dh-pkcs-parameter-gen") - CT (CKM_X9_42_DH_PARAMETER_GEN, "x9-42-dh-parameter-gen") - { 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 }, - { p11_constant_mechanisms, ELEMS (p11_constant_mechanisms) - 1 }, - { p11_constant_states, ELEMS (p11_constant_states) - 1 }, - { p11_constant_users, ELEMS (p11_constant_users) - 1 }, - { p11_constant_returns, ELEMS (p11_constant_returns) - 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->nicks[0] : NULL; -} - -p11_dict * -p11_constant_reverse (bool nick) -{ - const p11_constant *table; - p11_dict *lookups; - int length = -1; - int i, j, k; - - 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 (nick) { - for (k = 0; table[j].nicks[k] != NULL; k++) { - if (!p11_dict_set (lookups, (void *)table[j].nicks[k], - (void *)&table[j].value)) - return_val_if_reached (NULL); - } - } else { - if (!p11_dict_set (lookups, (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 deleted file mode 100644 index 1526373..0000000 --- a/common/constants.h +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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 *nicks[4]; -} 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[]; - -extern const p11_constant p11_constant_mechanisms[]; - -extern const p11_constant p11_constant_states[]; - -extern const p11_constant p11_constant_users[]; - -extern const p11_constant p11_constant_returns[]; - -#endif /* P11_CONSTANTS_H_ */ diff --git a/common/debug.c b/common/debug.c deleted file mode 100644 index 47933fa..0000000 --- a/common/debug.c +++ /dev/null @@ -1,158 +0,0 @@ -/* - * 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. - * - * - * CONTRIBUTORS - * Stef Walter <stef@memberwebs.com> - */ - -#include "config.h" - -#include "compat.h" -#include "debug.h" - -#include <assert.h> -#include <stdio.h> -#include <stdarg.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> - -struct DebugKey { - const char *name; - int value; -}; - -static struct DebugKey debug_keys[] = { - { "lib", P11_DEBUG_LIB }, - { "conf", P11_DEBUG_CONF }, - { "uri", P11_DEBUG_URI }, - { "proxy", P11_DEBUG_PROXY }, - { "trust", P11_DEBUG_TRUST }, - { "tool", P11_DEBUG_TOOL }, - { "rpc", P11_DEBUG_RPC }, - { 0, } -}; - -static bool debug_inited = false; -static bool debug_strict = false; - -/* global variable exported in debug.h */ -int p11_debug_current_flags = ~0; - -static int -parse_environ_flags (void) -{ - const char *env; - int result = 0; - const char *p; - const char *q; - int i; - - env = secure_getenv ("P11_KIT_STRICT"); - if (env && env[0] != '\0') - debug_strict = true; - - env = getenv ("P11_KIT_DEBUG"); - if (!env) - return 0; - - if (strcmp (env, "all") == 0) { - for (i = 0; debug_keys[i].name; i++) - result |= debug_keys[i].value; - - } else if (strcmp (env, "help") == 0) { - fprintf (stderr, "Supported debug values:"); - for (i = 0; debug_keys[i].name; i++) - fprintf (stderr, " %s", debug_keys[i].name); - fprintf (stderr, "\n"); - - } else { - p = env; - while (*p) { - q = strpbrk (p, ":;, \t"); - if (!q) - q = p + strlen (p); - - for (i = 0; debug_keys[i].name; i++) { - if (q - p == strlen (debug_keys[i].name) && - strncmp (debug_keys[i].name, p, q - p) == 0) - result |= debug_keys[i].value; - } - - p = q; - if (*p) - p++; - } - } - - return result; -} - -void -p11_debug_init (void) -{ - p11_debug_current_flags = parse_environ_flags (); - debug_inited = true; -} - -void -p11_debug_message (int flag, - const char *format, ...) -{ - va_list args; - - if (flag & p11_debug_current_flags) { - fprintf (stderr, "(p11-kit:%d) ", getpid()); - va_start (args, format); - vfprintf (stderr, format, args); - va_end (args); - fprintf (stderr, "\n"); - } -} - -void -p11_debug_precond (const char *format, - ...) -{ - va_list va; - - va_start (va, format); - vfprintf (stderr, format, va); - va_end (va); - -#ifdef __COVERITY__ - fprintf (stderr, "ignoring P11_KIT_STRICT under coverity: %d", (int)debug_strict); -#else - if (debug_strict) -#endif - abort (); -} diff --git a/common/debug.h b/common/debug.h deleted file mode 100644 index 6106f19..0000000 --- a/common/debug.h +++ /dev/null @@ -1,145 +0,0 @@ -/* - * 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_DEBUG_H -#define P11_DEBUG_H - -#include "compat.h" - -/* Please keep this enum in sync with keys in debug.c */ -enum { - P11_DEBUG_LIB = 1 << 1, - P11_DEBUG_CONF = 1 << 2, - P11_DEBUG_URI = 1 << 3, - P11_DEBUG_PROXY = 1 << 4, - P11_DEBUG_TRUST = 1 << 5, - P11_DEBUG_TOOL = 1 << 6, - P11_DEBUG_RPC = 1 << 7, -}; - -extern int p11_debug_current_flags; - -void p11_debug_init (void); - -void p11_debug_message (int flag, - const char *format, - ...) GNUC_PRINTF (2, 3); - -void p11_debug_precond (const char *format, - ...) GNUC_PRINTF (1, 2) - CLANG_ANALYZER_NORETURN; - -#ifndef assert_not_reached -#define assert_not_reached() \ - (assert (false && "this code should not be reached")) -#endif - -#define return_val_if_fail(x, v) \ - do { if (!(x)) { \ - p11_debug_precond ("p11-kit: '%s' not true at %s\n", #x, __func__); \ - return v; \ - } } while (false) - -#define return_if_fail(x) \ - do { if (!(x)) { \ - p11_debug_precond ("p11-kit: '%s' not true at %s\n", #x, __func__); \ - return; \ - } } while (false) - -#define return_if_reached() \ - do { \ - p11_debug_precond ("p11-kit: shouldn't be reached at %s\n", __func__); \ - return; \ - } while (false) - -#define return_val_if_reached(v) \ - do { \ - p11_debug_precond ("p11-kit: shouldn't be reached at %s\n", __func__); \ - return v; \ - } while (false) - -#define warn_if_reached(v) \ - do { \ - p11_debug_precond ("p11-kit: shouldn't be reached at %s\n", __func__); \ - } while (false) - -#define warn_if_fail(x) \ - do { if (!(x)) { \ - p11_debug_precond ("p11-kit: '%s' not true at %s\n", #x, __func__); \ - } } while (false) - -#endif /* DEBUG_H */ - -/* ----------------------------------------------------------------------------- - * Below this point is outside the DEBUG_H guard - so it can take effect - * more than once. So you can do: - * - * #define P11_DEBUG_FLAG P11_DEBUG_ONE_THING - * #include "debug.h" - * ... - * p11_debug ("if we're debugging one thing"); - * ... - * #undef P11_DEBUG_FLAG - * #define P11_DEBUG_FLAG DEBUG_OTHER_THING - * #include "debug.h" - * ... - * p11_debug ("if we're debugging the other thing"); - * ... - */ - -#ifdef P11_DEBUG_FLAG -#ifdef WITH_DEBUG - -#undef p11_debug -#define p11_debug(format, ...) do { \ - if (P11_DEBUG_FLAG & p11_debug_current_flags) \ - p11_debug_message (P11_DEBUG_FLAG, "%s: " format, __PRETTY_FUNCTION__, ##__VA_ARGS__); \ - } while (0) - -#undef p11_debugging -#define p11_debugging \ - (P11_DEBUG_FLAG & p11_debug_current_flags) - -#else /* !defined (WITH_DEBUG) */ - -#undef p11_debug -#define p11_debug(format, ...) \ - do {} while (false) - -#undef p11_debugging -#define p11_debugging (0) - -#endif /* !defined (WITH_DEBUG) */ - -#endif /* defined (P11_DEBUG_FLAG) */ diff --git a/common/dict.c b/common/dict.c deleted file mode 100644 index b7ab00d..0000000 --- a/common/dict.c +++ /dev/null @@ -1,389 +0,0 @@ -/* - * Copyright (c) 2004 Stefan Walter - * 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. - */ - -#include "config.h" - -#include "debug.h" -#include "dict.h" -#include "hash.h" - -#include <sys/types.h> - -#include <assert.h> -#include <stdint.h> -#include <stdlib.h> -#include <string.h> - -struct _p11_dict { - p11_dict_hasher hash_func; - p11_dict_equals equal_func; - p11_destroyer key_destroy_func; - p11_destroyer value_destroy_func; - - struct _p11_dictbucket **buckets; - unsigned int num_items; - unsigned int num_buckets; -}; - -typedef struct _p11_dictbucket { - void *key; - unsigned int hashed; - void *value; - struct _p11_dictbucket *next; -} dictbucket; - -static dictbucket * -next_entry (p11_dictiter *iter) -{ - dictbucket *bucket = iter->next; - while (!bucket) { - if (iter->index >= iter->dict->num_buckets) - return NULL; - bucket = iter->dict->buckets[iter->index++]; - } - iter->next = bucket->next; - return bucket; -} - - -bool -p11_dict_next (p11_dictiter *iter, - void **key, - void **value) -{ - dictbucket *bucket = next_entry (iter); - if (bucket == NULL) - return false; - if (key) - *key = bucket->key; - if (value) - *value = bucket->value; - return true; -} - -void -p11_dict_iterate (p11_dict *dict, - p11_dictiter *iter) -{ - iter->dict = dict; - iter->index = 0; - iter->next = NULL; -} - -static dictbucket ** -lookup_or_create_bucket (p11_dict *dict, - const void *key, - bool create) -{ - dictbucket **bucketp; - unsigned int hash; - - /* Perform the hashing */ - hash = dict->hash_func (key); - - /* scan linked list */ - for (bucketp = &dict->buckets[hash % dict->num_buckets]; - *bucketp != NULL; bucketp = &(*bucketp)->next) { - if((*bucketp)->hashed == hash && dict->equal_func ((*bucketp)->key, key)) - break; - } - - if ((*bucketp) != NULL || !create) - return bucketp; - - /* add a new entry for non-NULL val */ - (*bucketp) = calloc (sizeof (dictbucket), 1); - - if (*bucketp != NULL) { - (*bucketp)->key = (void*)key; - (*bucketp)->hashed = hash; - dict->num_items++; - } - - return bucketp; -} - -void * -p11_dict_get (p11_dict *dict, - const void *key) -{ - dictbucket **bucketp; - - bucketp = lookup_or_create_bucket (dict, key, false); - if (bucketp && *bucketp) - return (void*)((*bucketp)->value); - else - return NULL; -} - -bool -p11_dict_set (p11_dict *dict, - void *key, - void *val) -{ - dictbucket **bucketp; - p11_dictiter iter; - dictbucket *bucket; - dictbucket **new_buckets; - unsigned int num_buckets; - - bucketp = lookup_or_create_bucket (dict, key, true); - if(bucketp && *bucketp) { - - /* Destroy the previous key */ - if ((*bucketp)->key && (*bucketp)->key != key && dict->key_destroy_func) - dict->key_destroy_func ((*bucketp)->key); - - /* Destroy the previous value */ - if ((*bucketp)->value && (*bucketp)->value != val && dict->value_destroy_func) - dict->value_destroy_func ((*bucketp)->value); - - /* replace entry */ - (*bucketp)->key = key; - (*bucketp)->value = val; - - /* check that the collision rate isn't too high */ - if (dict->num_items > dict->num_buckets) { - num_buckets = dict->num_buckets * 2 + 1; - new_buckets = (dictbucket **)calloc (sizeof (dictbucket *), num_buckets); - - /* Ignore failures, maybe we can expand later */ - if(new_buckets) { - p11_dict_iterate (dict, &iter); - while ((bucket = next_entry (&iter)) != NULL) { - unsigned int i = bucket->hashed % num_buckets; - bucket->next = new_buckets[i]; - new_buckets[i] = bucket; - } - - free (dict->buckets); - dict->buckets = new_buckets; - dict->num_buckets = num_buckets; - } - } - - return true; - } - - return_val_if_reached (false); -} - -bool -p11_dict_steal (p11_dict *dict, - const void *key, - void **stolen_key, - void **stolen_value) -{ - dictbucket **bucketp; - - bucketp = lookup_or_create_bucket (dict, key, false); - if (bucketp && *bucketp) { - dictbucket *old = *bucketp; - *bucketp = (*bucketp)->next; - --dict->num_items; - if (stolen_key) - *stolen_key = old->key; - if (stolen_value) - *stolen_value = old->value; - free (old); - return true; - } - - return false; - -} - -bool -p11_dict_remove (p11_dict *dict, - const void *key) -{ - void *old_key; - void *old_value; - - if (!p11_dict_steal (dict, key, &old_key, &old_value)) - return false; - - if (dict->key_destroy_func) - dict->key_destroy_func (old_key); - if (dict->value_destroy_func) - dict->value_destroy_func (old_value); - return true; -} - -void -p11_dict_clear (p11_dict *dict) -{ - dictbucket *bucket, *next; - unsigned int i; - - /* Free all entries in the array */ - for (i = 0; i < dict->num_buckets; ++i) { - bucket = dict->buckets[i]; - while (bucket != NULL) { - next = bucket->next; - if (dict->key_destroy_func) - dict->key_destroy_func (bucket->key); - if (dict->value_destroy_func) - dict->value_destroy_func (bucket->value); - free (bucket); - bucket = next; - } - } - - memset (dict->buckets, 0, dict->num_buckets * sizeof (dictbucket *)); - dict->num_items = 0; -} - -p11_dict * -p11_dict_new (p11_dict_hasher hash_func, - p11_dict_equals equal_func, - p11_destroyer key_destroy_func, - p11_destroyer value_destroy_func) -{ - p11_dict *dict; - - assert (hash_func); - assert (equal_func); - - dict = malloc (sizeof (p11_dict)); - if (dict) { - dict->hash_func = hash_func; - dict->equal_func = equal_func; - dict->key_destroy_func = key_destroy_func; - dict->value_destroy_func = value_destroy_func; - - dict->num_buckets = 9; - dict->buckets = (dictbucket **)calloc (sizeof (dictbucket *), dict->num_buckets); - if (!dict->buckets) { - free (dict); - return NULL; - } - - dict->num_items = 0; - } - - return dict; -} - -void -p11_dict_free (p11_dict *dict) -{ - dictbucket *bucket; - p11_dictiter iter; - - if (!dict) - return; - - p11_dict_iterate (dict, &iter); - while ((bucket = next_entry (&iter)) != NULL) { - if (dict->key_destroy_func) - dict->key_destroy_func (bucket->key); - if (dict->value_destroy_func) - dict->value_destroy_func (bucket->value); - free (bucket); - } - - if (dict->buckets) - free (dict->buckets); - - free (dict); -} - -unsigned int -p11_dict_size (p11_dict *dict) -{ - return dict->num_items; -} - -unsigned int -p11_dict_str_hash (const void *string) -{ - uint32_t hash; - p11_hash_murmur3 (&hash, string, strlen (string), NULL); - return hash; -} - -bool -p11_dict_str_equal (const void *string_one, - const void *string_two) -{ - assert (string_one); - assert (string_two); - - return strcmp (string_one, string_two) == 0; -} - -unsigned int -p11_dict_ulongptr_hash (const void *to_ulong) -{ - assert (to_ulong); - return (unsigned int)*((unsigned long*)to_ulong); -} - -bool -p11_dict_ulongptr_equal (const void *ulong_one, - const void *ulong_two) -{ - assert (ulong_one); - assert (ulong_two); - return *((unsigned long*)ulong_one) == *((unsigned long*)ulong_two); -} - -unsigned int -p11_dict_intptr_hash (const void *to_int) -{ - assert (to_int); - return (unsigned int)*((int*)to_int); -} - -bool -p11_dict_intptr_equal (const void *int_one, - const void *int_two) -{ - assert (int_one); - assert (int_two); - return *((int*)int_one) == *((int*)int_two); -} - -unsigned int -p11_dict_direct_hash (const void *ptr) -{ - return (unsigned int)(size_t)ptr; -} - -bool -p11_dict_direct_equal (const void *ptr_one, - const void *ptr_two) -{ - return ptr_one == ptr_two; -} diff --git a/common/dict.h b/common/dict.h deleted file mode 100644 index 080f6b8..0000000 --- a/common/dict.h +++ /dev/null @@ -1,180 +0,0 @@ -/* - * Copyright (c) 2004 Stefan Walter - * 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 Waler <stefw@collabora.co.uk> - */ - -#ifndef P11_DICT_H_ -#define P11_DICT_H_ - -#include "compat.h" - -/* - * ARGUMENT DOCUMENTATION - * - * dict: The dict - * key: Pointer to the key value - * val: Pointer to the value - * iter: A dict iterator - */ - - -/* ---------------------------------------------------------------------------------- - * TYPES - */ - -/* Abstract type for dicts. */ -typedef struct _p11_dict p11_dict; - -/* Type for scanning hash tables. */ -typedef struct _p11_dictiter { - p11_dict *dict; - struct _p11_dictbucket *next; - unsigned int index; -} p11_dictiter; - -typedef unsigned int (*p11_dict_hasher) (const void *data); - -typedef bool (*p11_dict_equals) (const void *one, - const void *two); - -#ifndef P11_DESTROYER_DEFINED -#define P11_DESTROYER_DEFINED - -typedef void (*p11_destroyer) (void *data); - -#endif - -/* ----------------------------------------------------------------------------- - * MAIN - */ - -/* - * p11_dict_create : Create a hash table - * - returns an allocated hashtable - */ -p11_dict * p11_dict_new (p11_dict_hasher hasher, - p11_dict_equals equals, - p11_destroyer key_destroyer, - p11_destroyer value_destroyer); - -/* - * p11_dict_free : Free a hash table - */ -void p11_dict_free (p11_dict *dict); - -/* - * p11_dict_size: Number of values in hash table - * - returns the number of entries in hash table - */ -unsigned int p11_dict_size (p11_dict *dict); - -/* - * p11_dict_get: Retrieves a value from the hash table - * - returns the value of the entry - */ -void* p11_dict_get (p11_dict *dict, - const void *key); - -/* - * p11_dict_set: Set a value in the hash table - * - returns true if the entry was added properly - */ -bool p11_dict_set (p11_dict *dict, - void *key, - void *value); - -/* - * p11_dict_remove: Remove a value from the hash table - * - returns true if the entry was found - */ -bool p11_dict_remove (p11_dict *dict, - const void *key); - -/* - * p11_dict_steal: Remove a value from the hash table without calling - * destroy funcs - * - returns true if the entry was found - */ -bool p11_dict_steal (p11_dict *dict, - const void *key, - void **stolen_key, - void **stolen_value); - -/* - * p11_dict_iterate: Start enumerating through the hash table - * - returns a hash iterator - */ -void p11_dict_iterate (p11_dict *dict, - p11_dictiter *iter); - -/* - * p11_dict_next: Enumerate through hash table - * - sets key and value to key and/or value - * - returns whether there was another entry - * - p11_dict_remove or p11_dict_steal is safe to use on - * the current key. - */ -bool p11_dict_next (p11_dictiter *iter, - void **key, - void **value); - -/* - * p11_dict_clear: Clear all values from has htable. - */ -void p11_dict_clear (p11_dict *dict); - -/* ----------------------------------------------------------------------------- - * KEY FUNCTIONS - */ - -unsigned int p11_dict_str_hash (const void *string); - -bool p11_dict_str_equal (const void *string_one, - const void *string_two); - -unsigned int p11_dict_ulongptr_hash (const void *to_ulong); - -bool p11_dict_ulongptr_equal (const void *ulong_one, - const void *ulong_two); - -unsigned int p11_dict_intptr_hash (const void *to_int); - -bool p11_dict_intptr_equal (const void *int_one, - const void *int_two); - -unsigned int p11_dict_direct_hash (const void *ptr); - -bool p11_dict_direct_equal (const void *ptr_one, - const void *ptr_two); - -#endif /* __P11_DICT_H__ */ diff --git a/common/frob-getauxval.c b/common/frob-getauxval.c deleted file mode 100644 index 02745be..0000000 --- a/common/frob-getauxval.c +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2013 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 "compat.h" - -#include <assert.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -int -main (int argc, - char *argv[]) -{ - unsigned long type = 0; - unsigned long ret; - - if (argc == 2) - type = atoi (argv[1]); - - if (type == 0) { - fprintf (stderr, "usage: frob-getauxval 23"); - abort (); - } - - ret = getauxval (type); - printf ("getauxval(%lu) == %lu\n", type, ret); - return (int)ret; -} diff --git a/common/frob-getenv.c b/common/frob-getenv.c deleted file mode 100644 index a36594a..0000000 --- a/common/frob-getenv.c +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2014 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 "compat.h" - -#include <assert.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -int -main (int argc, - char *argv[]) -{ - int ret; - const char *val; - -fprintf (stderr, "calling secure_getenv(%s) getenv(%s) = %s\n", argv[1], argv[1], getenv(argv[1])); - val = secure_getenv (argv[1]); - if (val == NULL) { - printf ("%s=NULL\n", argv[1]); - return 0; - } - - ret = atoi (val); - if (ret == 0) { - fprintf (stderr, "usage: frob-getenv VAR"); - abort (); - } - - printf ("%s=%d\n", argv[1], ret); - return ret; -} diff --git a/common/hash.c b/common/hash.c deleted file mode 100644 index 5572085..0000000 --- a/common/hash.c +++ /dev/null @@ -1,174 +0,0 @@ -/* - * Copyright (C) 2004, 2005, 2007, 2011 Internet Systems Consortium, Inc. ("ISC") - * Copyright (C) 2000, 2001, 2003 Internet Software Consortium. - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH - * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY - * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, - * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM - * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE - * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR - * PERFORMANCE OF THIS SOFTWARE. - */ - -/*! \file - * SHA-1 in C - * \author By Steve Reid <steve@edmweb.com> - * 100% Public Domain - * \verbatim - * Test Vectors - * "abc" - * A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D - * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" - * 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 - * A million repetitions of "a" - * 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F - * \endverbatim - */ - -#include "config.h" - -#include "hash.h" - -#include <assert.h> -#include <stdarg.h> -#include <stdint.h> -#include <string.h> - -/* This code is based on the public domain MurmurHash3 from Austin Appleby: - * http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp - * - * We use only the 32 bit variant, and slow it down a bit to support unaligned - * reads. - */ - -#if !defined(__cplusplus) && (__GNUC__ > 2) -#define GNUC_INLINE __attribute__((always_inline)) -#else -#define GNUC_INLINE -#endif - -GNUC_INLINE static inline uint32_t -rotl (uint32_t x, - int8_t r) -{ - return (x << r) | (x >> (32 - r)); -} - -/* - * Finalization mix - force all bits of a hash block to avalanche - */ - -GNUC_INLINE static inline uint32_t -fmix (uint32_t h) -{ - h ^= h >> 16; - h *= 0x85ebca6b; - h ^= h >> 13; - h *= 0xc2b2ae35; - h ^= h >> 16; - - return h; -} - - -void -p11_hash_murmur3 (void *hash, - const void *input, - size_t len, - ...) -{ - uint8_t overflow[4]; - const uint8_t *data; - va_list va; - uint32_t h1; - uint32_t k1; - uint32_t c1; - uint32_t c2; - - h1 = 42; /* arbitrary choice of seed */ - c1 = 0xcc9e2d51; - c2 = 0x1b873593; - data = input; - - /* body */ - - /* Mix 4 bytes at a time into the hash */ - va_start (va, len); - for (;;) { - if (len >= 4) { - memcpy (&k1, data, 4); - data += 4; - len -= 4; - - } else { - size_t num = len; - memcpy (overflow, data, len); - - while (num < 4) { - size_t part; - - data = va_arg (va, const void *); - if (!data) - break; - - /* Combine uint32 from old and new */ - len = va_arg (va, size_t); - part = 4 - num; - if (part > len) - part = len; - memcpy (overflow + num, data, part); - data += part; - len -= part; - num += part; - } - - if (num < 4) { - len = num; - break; - } - - memcpy (&k1, overflow, 4); - } - - k1 *= c1; - k1 = rotl (k1, 15); - k1 *= c2; - - h1 ^= k1; - h1 = rotl (h1, 13); - h1 = h1 * 5 + 0xe6546b64; - } - va_end (va); - - /* tail */ - - k1 = 0; - - switch (len) { - case 3: - k1 ^= overflow[2] << 16; - case 2: - k1 ^= overflow[1] << 8; - case 1: - k1 ^= overflow[0]; - k1 *= c1; - k1 = rotl (k1, 15); - k1 *= c2; - h1 ^= k1; - default: - break; - } - - /* finalization */ - - h1 ^= len; - h1 = fmix(h1); - - assert (sizeof (h1) == P11_HASH_MURMUR3_LEN); - memcpy (hash, &h1, sizeof (h1)); -} diff --git a/common/hash.h b/common/hash.h deleted file mode 100644 index 41371c6..0000000 --- a/common/hash.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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@redhat.com> - */ - -#ifndef P11_HASH_H_ -#define P11_HASH_H_ - -#include "compat.h" - -#define P11_HASH_MURMUR3_LEN 4 - -void p11_hash_murmur3 (void *hash, - const void *input, - size_t length, - ...) GNUC_NULL_TERMINATED; - -#endif /* P11_HASH_H_ */ diff --git a/common/lexer.c b/common/lexer.c deleted file mode 100644 index 6253492..0000000 --- a/common/lexer.c +++ /dev/null @@ -1,239 +0,0 @@ -/* - * Copyright (c) 2005 Stefan Walter - * Copyright (c) 2011 Collabora Ltd. - * Copyright (c) 2013 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. - * - * - * CONTRIBUTORS - * Stef Walter <stefw@redhat.com> - */ - -#include "config.h" - -#define P11_DEBUG_FLAG P11_DEBUG_CONF -#include "debug.h" -#include "lexer.h" -#include "message.h" - -#include <assert.h> -#include <ctype.h> -#include <errno.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -void -p11_lexer_init (p11_lexer *lexer, - const char *filename, - const char *data, - size_t length) -{ - return_if_fail (lexer != NULL); - - memset (lexer, 0, sizeof (p11_lexer)); - lexer->at = data; - lexer->remaining = length; - - return_if_fail (filename != NULL); - lexer->filename = strdup (filename); - return_if_fail (lexer->filename != NULL); -} - -static void -clear_state (p11_lexer *lexer) -{ - switch (lexer->tok_type) { - case TOK_FIELD: - free (lexer->tok.field.name); - free (lexer->tok.field.value); - break; - case TOK_SECTION: - free (lexer->tok.section.name); - break; - case TOK_PEM: - case TOK_EOF: - break; - } - - memset (&lexer->tok, 0, sizeof (lexer->tok)); - lexer->tok_type = TOK_EOF; - lexer->complained = false; -} - -bool -p11_lexer_next (p11_lexer *lexer, - bool *failed) -{ - const char *colon; - const char *value; - const char *line; - const char *end; - const char *pos; - char *part; - - return_val_if_fail (lexer != NULL, false); - - clear_state (lexer); - if (failed) - *failed = false; - - /* Go through lines and process them */ - while (lexer->remaining != 0) { - assert (lexer->remaining > 0); - - /* Is this line the start of a PEM block? */ - if (strncmp (lexer->at, "-----BEGIN ", 11) == 0) { - pos = strnstr (lexer->at, "\n-----END ", lexer->remaining); - if (pos != NULL) { - end = memchr (pos + 1, '\n', lexer->remaining - (pos - lexer->at) - 1); - if (end) - end += 1; - else - end = lexer->at + lexer->remaining; - lexer->tok_type = TOK_PEM; - lexer->tok.pem.begin = lexer->at; - lexer->tok.pem.length = end - lexer->at; - assert (end - lexer->at <= lexer->remaining); - lexer->remaining -= (end - lexer->at); - lexer->at = end; - return true; - } - - p11_lexer_msg (lexer, "invalid pem block: no ending line"); - if (failed) - *failed = true; - return false; - } - - line = lexer->at; - end = memchr (lexer->at, '\n', lexer->remaining); - if (end == NULL) { - end = lexer->at + lexer->remaining; - lexer->remaining = 0; - lexer->at = end; - } else { - assert ((end - lexer->at) + 1 <= lexer->remaining); - lexer->remaining -= (end - lexer->at) + 1; - lexer->at = end + 1; - } - - /* Strip whitespace from line */ - while (line != end && isspace (line[0])) - ++line; - while (line != end && isspace (*(end - 1))) - --end; - - /* Empty lines / comments at start */ - if (line == end || line[0] == '#') - continue; - - /* Is the the a section ? */ - if (line[0] == '[') { - if (*(end - 1) != ']') { - part = strndup (line, end - line); - p11_lexer_msg (lexer, "invalid section header: missing braces"); - free (part); - if (failed) - *failed = true; - return false; - } - - lexer->tok_type = TOK_SECTION; - lexer->tok.section.name = strndup (line + 1, (end - line) - 2); - return_val_if_fail (lexer->tok.section.name != NULL, false); - return true; - } - - /* Look for the break between name: value on the same line */ - colon = memchr (line, ':', end - line); - if (!colon) { - part = strndup (line, end - line); - p11_lexer_msg (lexer, "invalid field line: no colon"); - free (part); - if (failed) - *failed = true; - return false; - } - - /* Strip whitespace from name and value */ - value = colon + 1; - while (value != end && isspace (value[0])) - ++value; - while (line != colon && isspace (*(colon - 1))) - --colon; - - lexer->tok_type = TOK_FIELD; - lexer->tok.field.name = strndup (line, colon - line); - lexer->tok.field.value = strndup (value, end - value); - return_val_if_fail (lexer->tok.field.name && lexer->tok.field.value, false); - return true; - } - - return false; -} - -void -p11_lexer_done (p11_lexer *lexer) -{ - return_if_fail (lexer != NULL); - clear_state (lexer); - free (lexer->filename); - memset (lexer, 0, sizeof (p11_lexer)); -} - -void -p11_lexer_msg (p11_lexer *lexer, - const char *msg) -{ - return_if_fail (lexer != NULL); - - if (lexer->complained) - return; - - switch (lexer->tok_type) { - case TOK_FIELD: - p11_message ("%s: %s: %s", lexer->filename, - lexer->tok.field.name, msg); - break; - case TOK_SECTION: - p11_message ("%s: [%s]: %s", lexer->filename, - lexer->tok.section.name, msg); - break; - case TOK_PEM: - p11_message ("%s: BEGIN ...: %s", lexer->filename, msg); - break; - default: - p11_message ("%s: %s", lexer->filename, msg); - break; - } - - lexer->complained = true; -} diff --git a/common/lexer.h b/common/lexer.h deleted file mode 100644 index 9daf296..0000000 --- a/common/lexer.h +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright (c) 2005 Stefan Walter - * Copyright (c) 2011 Collabora Ltd. - * Copyright (c) 2013 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@redhat.com> - */ - -#ifndef P11_LEXER_H__ -#define P11_LEXER_H__ - -#include "compat.h" - -enum { - TOK_EOF = 0, - TOK_SECTION = 1, - TOK_FIELD, - TOK_PEM, -}; - -typedef struct { - char *filename; - const char *at; - int remaining; - int complained; - - int tok_type; - union { - struct { - char *name; - } section; - struct { - char *name; - char *value; - } field; - struct { - const char *begin; - size_t length; - } pem; - } tok; -} p11_lexer; - -void p11_lexer_init (p11_lexer *lexer, - const char *filename, - const char *data, - size_t length); - -bool p11_lexer_next (p11_lexer *lexer, - bool *failed); - -void p11_lexer_done (p11_lexer *lexer); - -void p11_lexer_msg (p11_lexer *lexer, - const char *msg); - -#endif /* P11_LEXER_H__ */ diff --git a/common/library.c b/common/library.c deleted file mode 100644 index 502ea98..0000000 --- a/common/library.c +++ /dev/null @@ -1,212 +0,0 @@ -/* - * Copyright (c) 2011 Collabora Ltd - * Copyright (c) 2012 Stef Walter - * - * 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. - * - * - * CONTRIBUTORS - * Stef Walter <stef@thewalter.net> - */ - -#include "config.h" - -#include "compat.h" -#define P11_DEBUG_FLAG P11_DEBUG_LIB -#include "debug.h" -#include "library.h" -#include "message.h" - -#include <assert.h> -#include <stdarg.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#define P11_MESSAGE_MAX 512 - -typedef struct { - char message[P11_MESSAGE_MAX]; -} p11_local; - -static p11_local * _p11_library_get_thread_local (void); - -p11_mutex_t p11_library_mutex; - -#ifdef OS_UNIX -pthread_once_t p11_library_once = PTHREAD_ONCE_INIT; -#endif - -unsigned int p11_forkid = 1; - -static char * -thread_local_message (void) -{ - p11_local *local; - local = _p11_library_get_thread_local (); - return local ? local->message : NULL; -} - -static char * -dont_store_message (void) -{ - return NULL; -} - -static void -uninit_common (void) -{ - p11_debug ("uninitializing library"); -} - -#ifdef OS_UNIX - -static pthread_key_t thread_local = 0; - -static p11_local * -_p11_library_get_thread_local (void) -{ - p11_local *local; - - p11_library_init_once (); - - local = pthread_getspecific (thread_local); - if (local == NULL) { - local = calloc (1, sizeof (p11_local)); - pthread_setspecific (thread_local, local); - } - - return local; -} - -static void -count_forks (void) -{ - /* Thread safe, executed in child, one thread exists */ - p11_forkid++; -} - -void -p11_library_init_impl (void) -{ - p11_debug_init (); - p11_debug ("initializing library"); - p11_mutex_init (&p11_library_mutex); - pthread_key_create (&thread_local, free); - p11_message_storage = thread_local_message; - - pthread_atfork (NULL, NULL, count_forks); -} - -void -p11_library_init (void) -{ - p11_library_init_once (); -} - -void -p11_library_uninit (void) -{ - uninit_common (); - - /* Some cleanup to pacify valgrind */ - free (pthread_getspecific (thread_local)); - pthread_setspecific (thread_local, NULL); - - p11_message_storage = dont_store_message; - pthread_key_delete (thread_local); - p11_mutex_uninit (&p11_library_mutex); -} - -#endif /* OS_UNIX */ - -#ifdef OS_WIN32 - -static DWORD thread_local = TLS_OUT_OF_INDEXES; - -BOOL WINAPI DllMain (HINSTANCE, DWORD, LPVOID); - -static p11_local * -_p11_library_get_thread_local (void) -{ - LPVOID data; - - if (thread_local == TLS_OUT_OF_INDEXES) - return NULL; - - data = TlsGetValue (thread_local); - if (data == NULL) { - data = LocalAlloc (LPTR, sizeof (p11_local)); - TlsSetValue (thread_local, data); - } - - return (p11_local *)data; -} - -void -p11_library_init (void) -{ - p11_debug_init (); - p11_debug ("initializing library"); - p11_mutex_init (&p11_library_mutex); - thread_local = TlsAlloc (); - if (thread_local == TLS_OUT_OF_INDEXES) - p11_debug ("couldn't setup tls"); - else - p11_message_storage = thread_local_message; -} - -void -p11_library_thread_cleanup (void) -{ - p11_local *local; - if (thread_local != TLS_OUT_OF_INDEXES) { - p11_debug ("thread stopped, freeing tls"); - local = TlsGetValue (thread_local); - LocalFree (local); - } -} - -void -p11_library_uninit (void) -{ - LPVOID data; - - uninit_common (); - - if (thread_local != TLS_OUT_OF_INDEXES) { - p11_message_storage = dont_store_message; - data = TlsGetValue (thread_local); - LocalFree (data); - TlsFree (thread_local); - } - p11_mutex_uninit (&p11_library_mutex); -} - -#endif /* OS_WIN32 */ diff --git a/common/library.h b/common/library.h deleted file mode 100644 index f87494d..0000000 --- a/common/library.h +++ /dev/null @@ -1,74 +0,0 @@ -/* - * 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. - * - * - * CONTRIBUTORS - * Stef Walter <stef@memberwebs.com> - */ - -#ifndef P11_LIBRARY_H_ -#define P11_LIBRARY_H_ - -#include "config.h" -#include "compat.h" - -#include <sys/types.h> - -extern p11_mutex_t p11_library_mutex; - -extern unsigned int p11_forkid; - -#define p11_lock() p11_mutex_lock (&p11_library_mutex); - -#define p11_unlock() p11_mutex_unlock (&p11_library_mutex); - -#ifdef OS_WIN32 - -/* No implementation, because done by DllMain */ -#define p11_library_init_once() - -#else /* !OS_WIN32 */ -extern pthread_once_t p11_library_once; - -#define p11_library_init_once() \ - pthread_once (&p11_library_once, p11_library_init_impl); - -void p11_library_init_impl (void); - -#endif /* !OS_WIN32 */ - -void p11_library_init (void); - -void p11_library_thread_cleanup (void); - -void p11_library_uninit (void); - -#endif /* P11_LIBRARY_H_ */ diff --git a/common/message.c b/common/message.c deleted file mode 100644 index 35f2764..0000000 --- a/common/message.c +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Copyright (c) 2011 Collabora Ltd - * Copyright (c) 2012 Stef Walter - * - * 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. - * - * - * CONTRIBUTORS - * Stef Walter <stef@thewalter.net> - */ - -#include "config.h" - -/* - * Oh god. glibc is nasty. Changes behavior and definitions of POSIX - * functions to completely different signatures depending on defines - */ -#define _POSIX_C_SOURCE 200112L - -#include "compat.h" -#define P11_DEBUG_FLAG P11_DEBUG_LIB -#include "debug.h" -#include "message.h" - -#include <assert.h> -#include <stdarg.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -static bool print_messages = true; - -static char * -default_message_storage (void) -{ - static char message[P11_MESSAGE_MAX] = { 0, }; - return message; -} - -/* Function pointer declared in message.h as extern */ -char * (* p11_message_storage) (void) = default_message_storage; - -void -p11_message_store (const char* msg, - size_t length) -{ - char *buffer; - - /* - * p11_message_storage() is called to get a storage location for - * the last message. It defaults to a globally allocated buffer - * but is overridden in library.c with a function that returns - * per thread buffers. - * - * The returned value is P11_MESSAGE_MAX bytes long - */ - buffer = p11_message_storage (); - - if (length > P11_MESSAGE_MAX - 1) - length = P11_MESSAGE_MAX - 1; - - if (buffer != NULL) { - memcpy (buffer, msg, length); - buffer[length] = 0; - } -} - -void -p11_message_err (int errnum, - const char* msg, - ...) -{ - char buffer[P11_MESSAGE_MAX]; - char strerr[P11_MESSAGE_MAX]; - va_list va; - size_t length; - - va_start (va, msg); - length = vsnprintf (buffer, P11_MESSAGE_MAX - 1, msg, va); - va_end (va); - - /* Was it truncated? */ - if (length > P11_MESSAGE_MAX - 1) - length = P11_MESSAGE_MAX - 1; - buffer[length] = 0; - - strncpy (strerr, "Unknown error", sizeof (strerr)); - strerror_r (errnum, strerr, sizeof (strerr)); - strerr[P11_MESSAGE_MAX - 1] = 0; - - p11_message ("%s: %s", buffer, strerr); -} - -void -p11_message (const char* msg, - ...) -{ - char buffer[P11_MESSAGE_MAX]; - va_list va; - size_t length; - - va_start (va, msg); - length = vsnprintf (buffer, P11_MESSAGE_MAX - 1, msg, va); - va_end (va); - - /* Was it truncated? */ - if (length > P11_MESSAGE_MAX - 1) - length = P11_MESSAGE_MAX - 1; - buffer[length] = 0; - - /* If printing is not disabled, just print out */ - if (print_messages) - fprintf (stderr, "p11-kit: %s\n", buffer); - else - p11_debug_message (P11_DEBUG_LIB, "message: %s", buffer); - p11_message_store (buffer, length); -} - -void -p11_message_quiet (void) -{ - print_messages = false; -} - -void -p11_message_loud (void) -{ - print_messages = true; -} - -const char * -p11_message_last (void) -{ - char *buffer; - buffer = p11_message_storage (); - return buffer && buffer[0] ? buffer : NULL; -} - -void -p11_message_clear (void) -{ - char *buffer; - buffer = p11_message_storage (); - if (buffer != NULL) - buffer[0] = 0; -} diff --git a/common/message.h b/common/message.h deleted file mode 100644 index 3fe86df..0000000 --- a/common/message.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * 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. - * - * - * CONTRIBUTORS - * Stef Walter <stef@memberwebs.com> - */ - -#ifndef P11_MESSAGE_H_ -#define P11_MESSAGE_H_ - -#include "compat.h" - -#include <sys/types.h> - -#define P11_MESSAGE_MAX 512 - -extern char * (* p11_message_storage) (void); - -void p11_message (const char* msg, - ...) GNUC_PRINTF (1, 2); - -void p11_message_err (int errnum, - const char* msg, - ...) GNUC_PRINTF (2, 3); - -void p11_message_store (const char* msg, - size_t length); - -const char * p11_message_last (void); - -void p11_message_clear (void); - -void p11_message_quiet (void); - -void p11_message_loud (void); - -#endif /* P11_MESSAGE_H_ */ diff --git a/common/mock.c b/common/mock.c deleted file mode 100644 index c3f2503..0000000 --- a/common/mock.c +++ /dev/null @@ -1,3975 +0,0 @@ -/* - * 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> - */ - -#include "config.h" - -#include "debug.h" -#define CRYPTOKI_EXPORTS -#include "pkcs11.h" -#include "message.h" - -#include "mock.h" - -#include "attrs.h" -#define P11_DEBUG_FLAG P11_DEBUG_LIB -#include "debug.h" -#include "dict.h" -#include "array.h" -#include "library.h" - -#include <assert.h> -#include <ctype.h> -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> - -/* ------------------------------------------------------------------- - * GLOBALS and SUPPORT STUFF - */ - -/* Various mutexes */ -static p11_mutex_t init_mutex; - -/* Whether we've been initialized, and on what process id it happened */ -static bool pkcs11_initialized = false; -static pid_t pkcs11_initialized_pid = 0; - -static CK_UTF8CHAR *the_pin = NULL; -static CK_ULONG n_the_pin = 0; - -static bool logged_in = false; -static CK_USER_TYPE the_user_type = 0; - -typedef struct _Session { - CK_SESSION_HANDLE handle; - p11_dict *objects; - CK_SESSION_INFO info; - - /* For find operations */ - bool finding; - p11_array *matches; - - bool want_context_login; - - /* For encrypt, decrypt operations */ - CK_OBJECT_HANDLE crypto_key; - CK_ATTRIBUTE_TYPE crypto_method; - CK_MECHANISM_TYPE crypto_mechanism; - - /* For sign, verify, digest, CKM_MOCK_COUNT */ - CK_MECHANISM_TYPE hash_mechanism; - CK_ATTRIBUTE_TYPE hash_method; - CK_OBJECT_HANDLE hash_key; - CK_ULONG hash_count; - - /* For 'signing' with CKM_MOCK_PREFIX */ - CK_BYTE sign_prefix[128]; - CK_ULONG n_sign_prefix; - - /* The random seed */ - CK_BYTE random_seed[128]; - CK_ULONG random_seed_len; -} Session; - -static unsigned int unique_identifier = 100; -static p11_dict *the_sessions = NULL; -static p11_dict *the_objects = NULL; - -#define SIGNED_PREFIX "signed-prefix:" - -#define handle_to_pointer(handle) \ - ((void *)(size_t)(handle)) - -#define pointer_to_handle(pointer) \ - ((CK_ULONG)(size_t)(pointer)) - -static void -free_session (void *data) -{ - Session *sess = (Session *)data; - if (sess) { - p11_dict_free (sess->objects); - p11_array_free (sess->matches); - } - free (sess); -} - -static CK_RV -lookup_object (Session *sess, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE **attrs, - p11_dict **table) -{ - CK_BBOOL priv; - - *attrs = p11_dict_get (the_objects, handle_to_pointer (object)); - if (*attrs) { - if (table) - *table = the_objects; - } else { - *attrs = p11_dict_get (sess->objects, handle_to_pointer (object)); - if (*attrs) { - if (table) - *table = sess->objects; - } - } - - if (!*attrs) - return CKR_OBJECT_HANDLE_INVALID; - else if (!logged_in && p11_attrs_find_bool (*attrs, CKA_PRIVATE, &priv) && priv) - return CKR_USER_NOT_LOGGED_IN; - - return CKR_OK; -} - -void -mock_module_add_object (CK_SLOT_ID slot_id, - const CK_ATTRIBUTE *attrs) -{ - CK_ATTRIBUTE *copy; - - return_if_fail (slot_id == MOCK_SLOT_ONE_ID); - return_if_fail (attrs != NULL); - - copy = p11_attrs_dup (attrs); - return_if_fail (copy != NULL); - - mock_module_take_object (slot_id, copy); -} - -void -mock_module_take_object (CK_SLOT_ID slot_id, - CK_ATTRIBUTE *attrs) -{ - CK_OBJECT_HANDLE object; - - return_if_fail (slot_id == MOCK_SLOT_ONE_ID); - return_if_fail (attrs != NULL); - - object = ++unique_identifier; - if (!p11_dict_set (the_objects, handle_to_pointer (object), attrs)) - return_if_reached (); -} - -static void -module_reset_objects (CK_SLOT_ID slot_id) -{ - return_if_fail (slot_id == MOCK_SLOT_ONE_ID); - - if (!the_objects) { - the_objects = p11_dict_new (p11_dict_direct_hash, - p11_dict_direct_equal, - NULL, p11_attrs_free); - return_if_fail (the_objects != NULL); - } - - p11_dict_clear (the_objects); - - /* Our token object */ - { - CK_OBJECT_CLASS klass = CKO_DATA; - char *label = "TEST LABEL"; - CK_ATTRIBUTE attrs[] = { - { CKA_CLASS, &klass, sizeof (klass) }, - { CKA_LABEL, label, strlen (label) }, - { CKA_INVALID, NULL, 0 }, - }; - p11_dict_set (the_objects, handle_to_pointer (MOCK_DATA_OBJECT), p11_attrs_dup (attrs)); - } - - /* Private capitalize key */ - { - CK_OBJECT_CLASS klass = CKO_PRIVATE_KEY; - char *label = "Private Capitalize Key"; - char *value = "value"; - CK_MECHANISM_TYPE type = CKM_MOCK_CAPITALIZE; - CK_BBOOL btrue = CK_TRUE; - CK_ATTRIBUTE attrs[] = { - { CKA_CLASS, &klass, sizeof (klass) }, - { CKA_LABEL, label, strlen (label) }, - { CKA_ALLOWED_MECHANISMS, &type, sizeof (type) }, - { CKA_DECRYPT, &btrue, sizeof (btrue) }, - { CKA_PRIVATE, &btrue, sizeof (btrue) }, - { CKA_WRAP, &btrue, sizeof (btrue) }, - { CKA_UNWRAP, &btrue, sizeof (btrue) }, - { CKA_DERIVE, &btrue, sizeof (btrue) }, - { CKA_VALUE, value, strlen (value) }, - { CKA_INVALID, NULL, 0 }, - }; - p11_dict_set (the_objects, handle_to_pointer (MOCK_PRIVATE_KEY_CAPITALIZE), p11_attrs_dup (attrs)); - - } - - { - CK_OBJECT_CLASS klass = CKO_PUBLIC_KEY; - char *label = "Public Capitalize Key"; - char *value = "value"; - CK_MECHANISM_TYPE type = CKM_MOCK_CAPITALIZE; - CK_BBOOL btrue = CK_TRUE; - CK_BBOOL bfalse = CK_FALSE; - CK_ATTRIBUTE attrs[] = { - { CKA_CLASS, &klass, sizeof (klass) }, - { CKA_LABEL, label, strlen (label) }, - { CKA_ALLOWED_MECHANISMS, &type, sizeof (type) }, - { CKA_ENCRYPT, &btrue, sizeof (btrue) }, - { CKA_PRIVATE, &bfalse, sizeof (bfalse) }, - { CKA_VALUE, value, strlen (value) }, - { CKA_INVALID, NULL, 0 }, - }; - p11_dict_set (the_objects, handle_to_pointer (MOCK_PUBLIC_KEY_CAPITALIZE), p11_attrs_dup (attrs)); - - } - - { - CK_OBJECT_CLASS klass = CKO_PRIVATE_KEY; - char *label = "Private prefix key"; - char *value = "value"; - CK_MECHANISM_TYPE type = CKM_MOCK_PREFIX; - CK_BBOOL btrue = CK_TRUE; - CK_ATTRIBUTE attrs[] = { - { CKA_CLASS, &klass, sizeof (klass) }, - { CKA_LABEL, label, strlen (label) }, - { CKA_ALLOWED_MECHANISMS, &type, sizeof (type) }, - { CKA_SIGN, &btrue, sizeof (btrue) }, - { CKA_PRIVATE, &btrue, sizeof (btrue) }, - { CKA_ALWAYS_AUTHENTICATE, &btrue, sizeof (btrue) }, - { CKA_VALUE, value, strlen (value) }, - { CKA_INVALID, NULL, 0 }, - }; - p11_dict_set (the_objects, handle_to_pointer (MOCK_PRIVATE_KEY_PREFIX), p11_attrs_dup (attrs)); - - } - - { - CK_OBJECT_CLASS klass = CKO_PUBLIC_KEY; - char *label = "Public prefix key"; - char *value = "value"; - CK_MECHANISM_TYPE type = CKM_MOCK_PREFIX; - CK_BBOOL btrue = CK_TRUE; - CK_BBOOL bfalse = CK_FALSE; - CK_ATTRIBUTE attrs[] = { - { CKA_CLASS, &klass, sizeof (klass) }, - { CKA_LABEL, label, strlen (label) }, - { CKA_ALLOWED_MECHANISMS, &type, sizeof (type) }, - { CKA_VERIFY, &btrue, sizeof (btrue) }, - { CKA_PRIVATE, &bfalse, sizeof (bfalse) }, - { CKA_ALWAYS_AUTHENTICATE, &btrue, sizeof (btrue) }, - { CKA_VALUE, value, strlen (value) }, - { CKA_INVALID, NULL, 0 }, - }; - p11_dict_set (the_objects, handle_to_pointer (MOCK_PUBLIC_KEY_PREFIX), p11_attrs_dup (attrs)); - - } -} - -static void -module_finalize (void) -{ - p11_mutex_lock (&init_mutex); - - /* This should stop all other calls in */ - pkcs11_initialized = false; - pkcs11_initialized_pid = 0; - - if (the_objects) - p11_dict_free (the_objects); - the_objects = NULL; - - if (the_sessions) - p11_dict_free (the_sessions); - the_sessions = NULL; - logged_in = false; - the_user_type = 0; - - free (the_pin); - the_pin = NULL; - n_the_pin = 0; - - p11_mutex_unlock (&init_mutex); -} - -bool -mock_module_initialized (void) -{ - return pkcs11_initialized; -} -void -mock_module_reset (void) -{ - module_finalize (); - module_reset_objects (MOCK_SLOT_ONE_ID); - -} - -void -mock_module_enumerate_objects (CK_SESSION_HANDLE handle, - mock_enumerator func, - void *user_data) -{ - p11_dictiter iter; - void *key; - void *value; - Session *sess; - - assert (the_objects != NULL); - assert (func != NULL); - - /* Token objects */ - p11_dict_iterate (the_objects, &iter); - while (p11_dict_next (&iter, &key, &value)) { - if (!(func) (pointer_to_handle (key), value, user_data)) - return; - } - - /* session objects */ - if (handle) { - sess = p11_dict_get (the_sessions, handle_to_pointer (handle)); - if (sess) { - p11_dict_iterate (sess->objects, &iter); - while (p11_dict_next (&iter, &key, &value)) { - if (!(func) (pointer_to_handle (key), value, user_data)) - return; - } - } - } -} - -/* ------------------------------------------------------------------- - * INITIALIZATION and 'GLOBAL' CALLS - */ - -CK_RV -mock_C_Initialize (CK_VOID_PTR init_args) -{ - CK_C_INITIALIZE_ARGS_PTR args = NULL; - CK_RV ret = CKR_OK; - pid_t pid; - - p11_mutex_lock (&init_mutex); - - if (init_args != NULL) { - int supplied_ok; - - /* pReserved must be NULL */ - args = init_args; - - /* ALL supplied function pointers need to have the value either NULL or non-NULL. */ - supplied_ok = (args->CreateMutex == NULL && args->DestroyMutex == NULL && - args->LockMutex == NULL && args->UnlockMutex == NULL) || - (args->CreateMutex != NULL && args->DestroyMutex != NULL && - args->LockMutex != NULL && args->UnlockMutex != NULL); - if (!supplied_ok) { - p11_debug_precond ("invalid set of mutex calls supplied\n"); - ret = CKR_ARGUMENTS_BAD; - goto done; - } - - /* - * When the CKF_OS_LOCKING_OK flag isn't set return an error. - * We must be able to use our pthread functionality. - */ - if (!(args->flags & CKF_OS_LOCKING_OK)) { - p11_debug_precond ("can't do without os locking\n"); - ret = CKR_CANT_LOCK; - goto done; - } - } - - pid = getpid (); - if (pkcs11_initialized) { - - /* This process has called C_Initialize already */ - if (pid == pkcs11_initialized_pid) { - p11_debug_precond ("p11-kit: C_Initialize called twice for same process\n"); - ret = CKR_CRYPTOKI_ALREADY_INITIALIZED; - goto done; - } - } - - /* We store CK_ULONG as pointers here, so verify that they fit */ - assert (sizeof (CK_ULONG) <= sizeof (void *)); - - free (the_pin); - the_pin = (CK_UTF8CHAR_PTR)strdup ("booo"); - n_the_pin = 4; - - if (the_sessions) - p11_dict_free (the_sessions); - the_sessions = p11_dict_new (p11_dict_direct_hash, - p11_dict_direct_equal, - NULL, free_session); - - module_reset_objects (MOCK_SLOT_ONE_ID); - -done: - /* Mark us as officially initialized */ - if (ret == CKR_OK) { - pkcs11_initialized = true; - pkcs11_initialized_pid = pid; - } else if (ret != CKR_CRYPTOKI_ALREADY_INITIALIZED) { - pkcs11_initialized = false; - pkcs11_initialized_pid = 0; - } - - p11_mutex_unlock (&init_mutex); - - return ret; -} - -CK_RV -mock_X_Initialize (CK_X_FUNCTION_LIST *self, - CK_VOID_PTR init_args) -{ - return mock_C_Initialize (init_args); -} - -CK_RV -mock_C_Initialize__fails (CK_VOID_PTR init_args) -{ - return CKR_FUNCTION_FAILED; -} - -CK_RV -mock_X_Initialize__fails (CK_X_FUNCTION_LIST *self, - CK_VOID_PTR init_args) -{ - return mock_C_Initialize__fails (init_args); -} - -CK_RV -mock_C_Finalize (CK_VOID_PTR reserved) -{ - return_val_if_fail (pkcs11_initialized, CKR_CRYPTOKI_NOT_INITIALIZED); - return_val_if_fail (reserved == NULL, CKR_ARGUMENTS_BAD); - - module_finalize (); - return CKR_OK; -} - -CK_RV -mock_X_Finalize (CK_X_FUNCTION_LIST *self, - CK_VOID_PTR reserved) -{ - return mock_C_Finalize (reserved); -} - -CK_RV -mock_C_GetInfo (CK_INFO_PTR info) -{ - return_val_if_fail (info, CKR_ARGUMENTS_BAD); - - memcpy (info, &MOCK_INFO, sizeof (*info)); - return CKR_OK; -} - -CK_RV -mock_X_GetInfo (CK_X_FUNCTION_LIST *self, - CK_INFO_PTR info) -{ - return mock_C_GetInfo (info); -} - -CK_RV -mock_C_GetFunctionList_not_supported (CK_FUNCTION_LIST_PTR_PTR list) -{ - /* This would be a strange call to receive, should be overridden */ - return_val_if_reached (CKR_FUNCTION_NOT_SUPPORTED); -} - -CK_RV -mock_C_GetSlotList (CK_BBOOL token_present, - CK_SLOT_ID_PTR slot_list, - CK_ULONG_PTR count) -{ - CK_ULONG num; - - return_val_if_fail (count, CKR_ARGUMENTS_BAD); - - num = token_present ? 1 : 2; - - /* Application only wants to know the number of slots. */ - if (slot_list == NULL) { - *count = num; - return CKR_OK; - } - - if (*count < num) - return_val_if_reached (CKR_BUFFER_TOO_SMALL); - - *count = num; - slot_list[0] = MOCK_SLOT_ONE_ID; - if (!token_present) - slot_list[1] = MOCK_SLOT_TWO_ID; - - return CKR_OK; - -} - -CK_RV -mock_C_GetSlotList__no_tokens (CK_BBOOL token_present, - CK_SLOT_ID_PTR slot_list, - CK_ULONG_PTR count) -{ - return_val_if_fail (count, CKR_ARGUMENTS_BAD); - - /* No tokens */ - *count = 0; - return CKR_OK; -} - -CK_RV -mock_X_GetSlotList__no_tokens (CK_X_FUNCTION_LIST *self, - CK_BBOOL token_present, - CK_SLOT_ID_PTR slot_list, - CK_ULONG_PTR count) -{ - return mock_C_GetSlotList__no_tokens (token_present, - slot_list, - count); -; -} - -/* Update mock-module.h URIs when updating this */ - -static const CK_SLOT_INFO MOCK_INFO_ONE = { - "TEST SLOT ", - "TEST MANUFACTURER ", - CKF_TOKEN_PRESENT | CKF_REMOVABLE_DEVICE, - { 55, 155 }, - { 65, 165 }, -}; - -/* Update mock-module.h URIs when updating this */ - -static const CK_SLOT_INFO MOCK_INFO_TWO = { - "TEST SLOT ", - "TEST MANUFACTURER ", - CKF_REMOVABLE_DEVICE, - { 55, 155 }, - { 65, 165 }, -}; - -CK_RV -mock_C_GetSlotInfo (CK_SLOT_ID slot_id, - CK_SLOT_INFO_PTR info) -{ - return_val_if_fail (info, CKR_ARGUMENTS_BAD); - - if (slot_id == MOCK_SLOT_ONE_ID) { - memcpy (info, &MOCK_INFO_ONE, sizeof (*info)); - return CKR_OK; - } else if (slot_id == MOCK_SLOT_TWO_ID) { - memcpy (info, &MOCK_INFO_TWO, sizeof (*info)); - return CKR_OK; - } else { - return CKR_SLOT_ID_INVALID; - } -} - -CK_RV -mock_C_GetSlotList__fail_first (CK_BBOOL token_present, - CK_SLOT_ID_PTR slot_list, - CK_ULONG_PTR count) -{ - return CKR_VENDOR_DEFINED; -} - -CK_RV -mock_C_GetSlotList__fail_late (CK_BBOOL token_present, - CK_SLOT_ID_PTR slot_list, - CK_ULONG_PTR count) -{ - if (!slot_list) - return mock_C_GetSlotList (token_present, slot_list, count); - return CKR_VENDOR_DEFINED; -} - -CK_RV -mock_C_GetSlotInfo__invalid_slotid (CK_SLOT_ID id, - CK_SLOT_INFO_PTR info) -{ - return_val_if_fail (info, CKR_ARGUMENTS_BAD); - - return CKR_SLOT_ID_INVALID; -} - -CK_RV -mock_X_GetSlotInfo__invalid_slotid (CK_X_FUNCTION_LIST *self, - CK_SLOT_ID id, - CK_SLOT_INFO_PTR info) -{ - return_val_if_fail (info, CKR_ARGUMENTS_BAD); - - return CKR_SLOT_ID_INVALID; -} - -/* Update gck-mock.h URIs when updating this */ - -static const CK_TOKEN_INFO MOCK_TOKEN_ONE = { - "TEST LABEL ", - "TEST MANUFACTURER ", - "TEST MODEL ", - "TEST SERIAL ", - CKF_LOGIN_REQUIRED | CKF_USER_PIN_INITIALIZED | CKF_CLOCK_ON_TOKEN | CKF_TOKEN_INITIALIZED, - 1, - 2, - 3, - 4, - 5, - 6, - 7, - 8, - 9, - 10, - { 75, 175 }, - { 85, 185 }, - { '1', '9', '9', '9', '0', '5', '2', '5', '0', '9', '1', '9', '5', '9', '0', '0' } -}; - -CK_RV -mock_C_GetTokenInfo (CK_SLOT_ID slot_id, - CK_TOKEN_INFO_PTR info) -{ - return_val_if_fail (info != NULL, CKR_ARGUMENTS_BAD); - - if (slot_id == MOCK_SLOT_ONE_ID) { - memcpy (info, &MOCK_TOKEN_ONE, sizeof (*info)); - return CKR_OK; - } else if (slot_id == MOCK_SLOT_TWO_ID) { - return CKR_TOKEN_NOT_PRESENT; - } else { - return CKR_SLOT_ID_INVALID; - } -} - -CK_RV -mock_C_GetTokenInfo__invalid_slotid (CK_SLOT_ID slot_id, - CK_TOKEN_INFO_PTR info) -{ - return_val_if_fail (info, CKR_ARGUMENTS_BAD); - - return CKR_SLOT_ID_INVALID; -} - -CK_RV -mock_X_GetTokenInfo__invalid_slotid (CK_X_FUNCTION_LIST *self, - CK_SLOT_ID slot_id, - CK_TOKEN_INFO_PTR info) -{ - return_val_if_fail (info, CKR_ARGUMENTS_BAD); - - return CKR_SLOT_ID_INVALID; -} - -/* - * TWO mechanisms: - * CKM_MOCK_CAPITALIZE - * CKM_MOCK_PREFIX - */ - -CK_RV -mock_C_GetMechanismList (CK_SLOT_ID slot_id, - CK_MECHANISM_TYPE_PTR mechanism_list, - CK_ULONG_PTR count) -{ - return_val_if_fail (count != NULL, CKR_ARGUMENTS_BAD); - - if (slot_id == MOCK_SLOT_TWO_ID) - return CKR_TOKEN_NOT_PRESENT; - else if (slot_id != MOCK_SLOT_ONE_ID) - return CKR_SLOT_ID_INVALID; - - /* Application only wants to know the number of slots. */ - if (mechanism_list == NULL) { - *count = 2; - return CKR_OK; - } - - if (*count < 2) - return_val_if_reached (CKR_BUFFER_TOO_SMALL); - - mechanism_list[0] = CKM_MOCK_CAPITALIZE; - mechanism_list[1] = CKM_MOCK_PREFIX; - *count = 2; - return CKR_OK; -} - -CK_RV -mock_C_GetTokenInfo__not_initialized (CK_SLOT_ID slot_id, - CK_TOKEN_INFO_PTR info) -{ - CK_RV rv; - - rv = mock_C_GetTokenInfo (slot_id, info); - if (rv == CKR_OK) - info->flags &= ~ CKF_TOKEN_INITIALIZED; - - return rv; -} - -/* - * TWO mechanisms: - * CKM_MOCK_CAPITALIZE - * CKM_MOCK_PREFIX - */ - -CK_RV -mock_C_GetMechanismList__invalid_slotid (CK_SLOT_ID id, - CK_MECHANISM_TYPE_PTR mechanism_list, - CK_ULONG_PTR count) -{ - return_val_if_fail (count, CKR_ARGUMENTS_BAD); - - return CKR_SLOT_ID_INVALID; -} - -CK_RV -mock_X_GetMechanismList__invalid_slotid (CK_X_FUNCTION_LIST *self, - CK_SLOT_ID id, - CK_MECHANISM_TYPE_PTR mechanism_list, - CK_ULONG_PTR count) -{ - return_val_if_fail (count, CKR_ARGUMENTS_BAD); - - return CKR_SLOT_ID_INVALID; -} - -static const CK_MECHANISM_INFO MOCK_MECH_CAPITALIZE = { - 512, 4096, CKF_ENCRYPT | CKF_DECRYPT -}; - -static const CK_MECHANISM_INFO MOCK_MECH_PREFIX = { - 2048, 2048, CKF_SIGN | CKF_VERIFY -}; - -CK_RV -mock_C_GetMechanismInfo (CK_SLOT_ID slot_id, - CK_MECHANISM_TYPE type, - CK_MECHANISM_INFO_PTR info) -{ - return_val_if_fail (info, CKR_ARGUMENTS_BAD); - - if (slot_id == MOCK_SLOT_TWO_ID) - return CKR_TOKEN_NOT_PRESENT; - else if (slot_id != MOCK_SLOT_ONE_ID) - return CKR_SLOT_ID_INVALID; - - if (type == CKM_MOCK_CAPITALIZE) { - memcpy (info, &MOCK_MECH_CAPITALIZE, sizeof (*info)); - return CKR_OK; - } else if (type == CKM_MOCK_PREFIX) { - memcpy (info, &MOCK_MECH_PREFIX, sizeof (*info)); - return CKR_OK; - } else { - return CKR_MECHANISM_INVALID; - } -} - -CK_RV -mock_C_GetMechanismInfo__invalid_slotid (CK_SLOT_ID slot_id, - CK_MECHANISM_TYPE type, - CK_MECHANISM_INFO_PTR info) -{ - return_val_if_fail (info, CKR_ARGUMENTS_BAD); - - return CKR_SLOT_ID_INVALID; -} - -CK_RV -mock_X_GetMechanismInfo__invalid_slotid (CK_X_FUNCTION_LIST *self, - CK_SLOT_ID slot_id, - CK_MECHANISM_TYPE type, - CK_MECHANISM_INFO_PTR info) -{ - return_val_if_fail (info, CKR_ARGUMENTS_BAD); - - return CKR_SLOT_ID_INVALID; -} - -CK_RV -mock_C_InitToken__specific_args (CK_SLOT_ID slot_id, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len, - CK_UTF8CHAR_PTR label) -{ - return_val_if_fail (pin != NULL, CKR_ARGUMENTS_BAD); - return_val_if_fail (label != NULL, CKR_ARGUMENTS_BAD); - - if (slot_id == MOCK_SLOT_TWO_ID) - return CKR_TOKEN_NOT_PRESENT; - else if (slot_id != MOCK_SLOT_ONE_ID) - return CKR_SLOT_ID_INVALID; - - if (strlen ("TEST PIN") != pin_len || - strncmp ((char *)pin, "TEST PIN", pin_len) != 0) - return CKR_PIN_INVALID; - if (strcmp ((char *)label, "TEST LABEL") != 0) - return CKR_ARGUMENTS_BAD; - - free (the_pin); - the_pin = memdup (pin, pin_len); - return_val_if_fail (the_pin != NULL, CKR_HOST_MEMORY); - n_the_pin = pin_len; - return CKR_OK; -} - -CK_RV -mock_C_InitToken__invalid_slotid (CK_SLOT_ID slot_id, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len, - CK_UTF8CHAR_PTR label) -{ - return CKR_SLOT_ID_INVALID; -} - -CK_RV -mock_X_InitToken__invalid_slotid (CK_X_FUNCTION_LIST *self, - CK_SLOT_ID slot_id, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len, - CK_UTF8CHAR_PTR label) -{ - return CKR_SLOT_ID_INVALID; -} - -CK_RV -mock_C_WaitForSlotEvent (CK_FLAGS flags, - CK_SLOT_ID_PTR slot, - CK_VOID_PTR reserved) -{ - return_val_if_fail (slot, CKR_ARGUMENTS_BAD); - - if (flags & CKF_DONT_BLOCK) - return CKR_NO_EVENT; - - *slot = MOCK_SLOT_TWO_ID; - return CKR_OK; -} - -CK_RV -mock_C_WaitForSlotEvent__no_event (CK_FLAGS flags, - CK_SLOT_ID_PTR slot, - CK_VOID_PTR reserved) -{ - return_val_if_fail (slot, CKR_ARGUMENTS_BAD); - - return CKR_NO_EVENT; -} - -CK_RV -mock_X_WaitForSlotEvent__no_event (CK_X_FUNCTION_LIST *self, - CK_FLAGS flags, - CK_SLOT_ID_PTR slot, - CK_VOID_PTR reserved) -{ - return_val_if_fail (slot, CKR_ARGUMENTS_BAD); - - return CKR_NO_EVENT; -} - -CK_RV -mock_C_OpenSession (CK_SLOT_ID slot_id, - CK_FLAGS flags, - CK_VOID_PTR user_data, - CK_NOTIFY callback, - CK_SESSION_HANDLE_PTR session) -{ - Session *sess; - - return_val_if_fail (session, CKR_ARGUMENTS_BAD); - - if (slot_id == MOCK_SLOT_TWO_ID) - return CKR_TOKEN_NOT_PRESENT; - else if (slot_id != MOCK_SLOT_ONE_ID) - return CKR_SLOT_ID_INVALID; - if ((flags & CKF_SERIAL_SESSION) != CKF_SERIAL_SESSION) - return CKR_SESSION_PARALLEL_NOT_SUPPORTED; - - sess = calloc (1, sizeof (Session)); - sess->handle = ++unique_identifier; - sess->info.flags = flags; - sess->info.slotID = slot_id; - sess->info.state = 0; - sess->info.ulDeviceError = 1414; - sess->objects = p11_dict_new (p11_dict_direct_hash, p11_dict_direct_equal, - NULL, p11_attrs_free); - *session = sess->handle; - - memcpy (sess->random_seed, "random", 6); - sess->random_seed_len = 6; - - p11_dict_set (the_sessions, handle_to_pointer (sess->handle), sess); - return CKR_OK; -} - -CK_RV -mock_C_OpenSession__invalid_slotid (CK_SLOT_ID slot_id, - CK_FLAGS flags, - CK_VOID_PTR user_data, - CK_NOTIFY callback, - CK_SESSION_HANDLE_PTR session) -{ - return_val_if_fail (session, CKR_ARGUMENTS_BAD); - - return CKR_SLOT_ID_INVALID; -} - -CK_RV -mock_X_OpenSession__invalid_slotid (CK_X_FUNCTION_LIST *self, - CK_SLOT_ID slot_id, - CK_FLAGS flags, - CK_VOID_PTR user_data, - CK_NOTIFY callback, - CK_SESSION_HANDLE_PTR session) -{ - return_val_if_fail (session, CKR_ARGUMENTS_BAD); - - return CKR_SLOT_ID_INVALID; -} - -CK_RV -mock_C_OpenSession__fails (CK_SLOT_ID slot_id, - CK_FLAGS flags, - CK_VOID_PTR user_data, - CK_NOTIFY callback, - CK_SESSION_HANDLE_PTR session) -{ - return_val_if_fail (session, CKR_ARGUMENTS_BAD); - - return CKR_DEVICE_ERROR; -} - -CK_RV -mock_C_CloseSession (CK_SESSION_HANDLE session) -{ - Session *sess; - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - p11_dict_remove (the_sessions, handle_to_pointer (session)); - return CKR_OK; -} - -CK_RV -mock_C_CloseSession__invalid_handle (CK_SESSION_HANDLE session) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_CloseSession__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_CloseAllSessions (CK_SLOT_ID slot_id) -{ - if (slot_id == MOCK_SLOT_TWO_ID) - return CKR_TOKEN_NOT_PRESENT; - else if (slot_id != MOCK_SLOT_ONE_ID) - return CKR_SLOT_ID_INVALID; - - p11_dict_clear (the_sessions); - return CKR_OK; -} - -CK_RV -mock_C_CloseAllSessions__invalid_slotid (CK_SLOT_ID slot_id) -{ - return CKR_SLOT_ID_INVALID; -} - -CK_RV -mock_X_CloseAllSessions__invalid_slotid (CK_X_FUNCTION_LIST *self, - CK_SLOT_ID slot_id) -{ - return CKR_SLOT_ID_INVALID; -} - -CK_RV -mock_C_GetFunctionStatus (CK_SESSION_HANDLE session) -{ - if (!p11_dict_get (the_sessions, handle_to_pointer (session))) - return CKR_SESSION_HANDLE_INVALID; - return CKR_FUNCTION_NOT_PARALLEL; -} - -CK_RV -mock_C_GetFunctionStatus__not_parallel (CK_SESSION_HANDLE session) -{ - return CKR_FUNCTION_NOT_PARALLEL; -} - -CK_RV -mock_C_CancelFunction (CK_SESSION_HANDLE session) -{ - if (!p11_dict_get (the_sessions, handle_to_pointer (session))) - return CKR_SESSION_HANDLE_INVALID; - return CKR_FUNCTION_NOT_PARALLEL; -} - -CK_RV -mock_C_CancelFunction__not_parallel (CK_SESSION_HANDLE session) -{ - return CKR_FUNCTION_NOT_PARALLEL; -} - -CK_RV -mock_C_GetSessionInfo (CK_SESSION_HANDLE session, - CK_SESSION_INFO_PTR info) -{ - Session *sess; - - return_val_if_fail (info != NULL, CKR_ARGUMENTS_BAD); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - if (logged_in) { - if (sess->info.flags & CKF_RW_SESSION) - sess->info.state = CKS_RW_USER_FUNCTIONS; - else - sess->info.state = CKS_RO_USER_FUNCTIONS; - } else { - if (sess->info.flags & CKF_RW_SESSION) - sess->info.state = CKS_RW_PUBLIC_SESSION; - else - sess->info.state = CKS_RO_PUBLIC_SESSION; - } - - memcpy (info, &sess->info, sizeof (*info)); - return CKR_OK; -} - -CK_RV -mock_C_GetSessionInfo__invalid_handle (CK_SESSION_HANDLE session, - CK_SESSION_INFO_PTR info) -{ - return_val_if_fail (info, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_GetSessionInfo__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_SESSION_INFO_PTR info) -{ - return_val_if_fail (info, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_InitPIN__specific_args (CK_SESSION_HANDLE session, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len) -{ - Session *sess; - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (sess == NULL) - return CKR_SESSION_HANDLE_INVALID; - - if (strlen ("TEST PIN") != pin_len || - strncmp ((char *)pin, "TEST PIN", pin_len) != 0) - return CKR_PIN_INVALID; - - free (the_pin); - the_pin = memdup (pin, pin_len); - return_val_if_fail (the_pin != NULL, CKR_HOST_MEMORY); - n_the_pin = pin_len; - return CKR_OK; -} - -CK_RV -mock_C_InitPIN__invalid_handle (CK_SESSION_HANDLE session, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_InitPIN__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_SetPIN__specific_args (CK_SESSION_HANDLE session, - CK_UTF8CHAR_PTR old_pin, - CK_ULONG old_pin_len, - CK_UTF8CHAR_PTR new_pin, - CK_ULONG new_pin_len) -{ - Session *sess; - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (sess == NULL) - return CKR_SESSION_HANDLE_INVALID; - - if (old_pin_len != n_the_pin) - return CKR_PIN_INCORRECT; - if (memcmp (old_pin, the_pin, n_the_pin) != 0) - return CKR_PIN_INCORRECT; - - if (strlen ("TEST PIN") != new_pin_len || - strncmp ((char *)new_pin, "TEST PIN", new_pin_len) != 0) - return CKR_PIN_INVALID; - - free (the_pin); - the_pin = memdup (new_pin, new_pin_len); - return_val_if_fail (the_pin != NULL, CKR_HOST_MEMORY); - n_the_pin = new_pin_len; - return CKR_OK; -} - -CK_RV -mock_C_SetPIN__invalid_handle (CK_SESSION_HANDLE session, - CK_UTF8CHAR_PTR old_pin, - CK_ULONG old_pin_len, - CK_UTF8CHAR_PTR new_pin, - CK_ULONG new_pin_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_SetPIN__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_UTF8CHAR_PTR old_pin, - CK_ULONG old_pin_len, - CK_UTF8CHAR_PTR new_pin, - CK_ULONG new_pin_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_GetOperationState (CK_SESSION_HANDLE session, - CK_BYTE_PTR operation_state, - CK_ULONG_PTR operation_state_len) -{ - Session *sess; - - return_val_if_fail (operation_state_len, CKR_ARGUMENTS_BAD); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (sess == NULL) - return CKR_SESSION_HANDLE_INVALID; - - if (!operation_state) { - *operation_state_len = sizeof (sess); - return CKR_OK; - } - - if (*operation_state_len < sizeof (sess)) - return CKR_BUFFER_TOO_SMALL; - - memcpy (operation_state, &sess, sizeof (sess)); - *operation_state_len = sizeof (sess); - return CKR_OK; -} - -CK_RV -mock_C_GetOperationState__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR operation_state, - CK_ULONG_PTR operation_state_len) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -mock_X_GetOperationState__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR operation_state, - CK_ULONG_PTR operation_state_len) -{ - return CKR_FUNCTION_NOT_SUPPORTED; -} - -CK_RV -mock_C_SetOperationState (CK_SESSION_HANDLE session, - CK_BYTE_PTR operation_state, - CK_ULONG operation_state_len, - CK_OBJECT_HANDLE encryption_key, - CK_OBJECT_HANDLE authentication_key) -{ - Session *sess; - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (sess == NULL) - return CKR_SESSION_HANDLE_INVALID; - - if (!operation_state || operation_state_len != sizeof (sess)) - return CKR_ARGUMENTS_BAD; - - /* Yes, just arbitrary numbers, to make sure they got through */ - if (encryption_key != 355 || authentication_key != 455) - return CKR_KEY_HANDLE_INVALID; - if (memcmp (operation_state, &sess, sizeof (sess)) != 0) - return CKR_SAVED_STATE_INVALID; - return CKR_OK; -} - -CK_RV -mock_C_SetOperationState__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR operation_state, - CK_ULONG operation_state_len, - CK_OBJECT_HANDLE encryption_key, - CK_OBJECT_HANDLE authentication_key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_SetOperationState__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR operation_state, - CK_ULONG operation_state_len, - CK_OBJECT_HANDLE encryption_key, - CK_OBJECT_HANDLE authentication_key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_Login (CK_SESSION_HANDLE session, - CK_USER_TYPE user_type, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len) -{ - Session *sess; - - return_val_if_fail (user_type == CKU_SO || - user_type == CKU_USER || - user_type == CKU_CONTEXT_SPECIFIC, - CKR_USER_TYPE_INVALID); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (sess == NULL) - return CKR_SESSION_HANDLE_INVALID; - - if (logged_in && user_type != CKU_CONTEXT_SPECIFIC) - return CKR_USER_ALREADY_LOGGED_IN; - - if (!pin) - return CKR_PIN_INCORRECT; - - if (pin_len != n_the_pin) - return CKR_PIN_INCORRECT; - if (strncmp ((char *)pin, (char *)the_pin, pin_len) != 0) - return CKR_PIN_INCORRECT; - - if (user_type == CKU_CONTEXT_SPECIFIC) { - return_val_if_fail (sess->want_context_login, CKR_OPERATION_NOT_INITIALIZED); - sess->want_context_login = false; - } else { - logged_in = true; - the_user_type = user_type; - } - - return CKR_OK; -} - -CK_RV -mock_C_Login__invalid_handle (CK_SESSION_HANDLE session, - CK_USER_TYPE user_type, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_Login__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_USER_TYPE user_type, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_Logout (CK_SESSION_HANDLE session) -{ - Session *sess; - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - if (!logged_in) - return CKR_USER_NOT_LOGGED_IN; - - logged_in = false; - the_user_type = 0; - return CKR_OK; -} - -CK_RV -mock_C_Logout__invalid_handle (CK_SESSION_HANDLE session) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_Logout__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_CreateObject (CK_SESSION_HANDLE session, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR object) -{ - CK_ATTRIBUTE *attrs; - Session *sess; - CK_BBOOL token, priv; - - return_val_if_fail (object, CKR_ARGUMENTS_BAD); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - attrs = p11_attrs_buildn (NULL, template, count); - - if (p11_attrs_find_bool (attrs, CKA_PRIVATE, &priv) && priv) { - if (!logged_in) { - p11_attrs_free (attrs); - return CKR_USER_NOT_LOGGED_IN; - } - } - - *object = ++unique_identifier; - if (p11_attrs_find_bool (attrs, CKA_TOKEN, &token) && token) - p11_dict_set (the_objects, handle_to_pointer (*object), attrs); - else - p11_dict_set (sess->objects, handle_to_pointer (*object), attrs); - - return CKR_OK; -} - -CK_RV -mock_C_CreateObject__invalid_handle (CK_SESSION_HANDLE session, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR new_object) -{ - return_val_if_fail (new_object, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_CreateObject__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR new_object) -{ - return_val_if_fail (new_object, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_CopyObject (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR new_object) -{ - CK_ATTRIBUTE *attrs; - Session *sess; - CK_BBOOL token, priv; - CK_RV rv; - - return_val_if_fail (object, CKR_ARGUMENTS_BAD); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - rv = lookup_object (sess, object, &attrs, NULL); - if (rv != CKR_OK) - return rv; - - if (p11_attrs_find_bool (attrs, CKA_PRIVATE, &priv) && priv) { - if (!logged_in) - return CKR_USER_NOT_LOGGED_IN; - } - - attrs = p11_attrs_buildn (p11_attrs_dup (attrs), template, count); - - *new_object = ++unique_identifier; - if (p11_attrs_find_bool (attrs, CKA_TOKEN, &token) && token) - p11_dict_set (the_objects, handle_to_pointer (*new_object), attrs); - else - p11_dict_set (sess->objects, handle_to_pointer (*new_object), attrs); - - return CKR_OK; -} - -CK_RV -mock_C_CopyObject__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR new_object) -{ - return_val_if_fail (new_object, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - - -CK_RV -mock_X_CopyObject__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR new_object) -{ - return_val_if_fail (new_object, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_DestroyObject (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object) -{ - CK_ATTRIBUTE *attrs; - Session *sess; - p11_dict *table; - CK_RV rv; - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - rv = lookup_object (sess, object, &attrs, &table); - if (rv != CKR_OK) - return rv; - - p11_dict_remove (table, handle_to_pointer (object)); - return CKR_OK; -} - -CK_RV -mock_C_DestroyObject__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_DestroyObject__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_GetObjectSize (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ULONG_PTR size) -{ - CK_ATTRIBUTE *attrs; - Session *sess; - CK_RV rv; - CK_ULONG i; - - return_val_if_fail (size != NULL, CKR_ARGUMENTS_BAD); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - rv = lookup_object (sess, object, &attrs, NULL); - if (rv != CKR_OK) - return rv; - - *size = 0; - for (i = 0; !p11_attrs_terminator (attrs + i); i++) { - if (attrs[i].ulValueLen != (CK_ULONG)-1) - *size += attrs[i].ulValueLen; - } - - return CKR_OK; -} - -CK_RV -mock_C_GetObjectSize__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ULONG_PTR size) -{ - return_val_if_fail (size, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_GetObjectSize__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ULONG_PTR size) -{ - return_val_if_fail (size, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_GetAttributeValue (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count) -{ - CK_ATTRIBUTE *result; - CK_RV ret = CKR_OK; - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE *attr; - Session *sess; - CK_ULONG i; - CK_RV rv; - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (sess == NULL) - return CKR_SESSION_HANDLE_INVALID; - - rv = lookup_object (sess, object, &attrs, NULL); - if (rv != CKR_OK) - return rv; - - for (i = 0; i < count; ++i) { - result = template + i; - attr = p11_attrs_find (attrs, result->type); - if (!attr) { - result->ulValueLen = (CK_ULONG)-1; - ret = CKR_ATTRIBUTE_TYPE_INVALID; - continue; - } - - if (!result->pValue) { - result->ulValueLen = attr->ulValueLen; - continue; - } - - if (result->ulValueLen >= attr->ulValueLen) { - memcpy (result->pValue, attr->pValue, attr->ulValueLen); - result->ulValueLen = attr->ulValueLen; - continue; - } - - result->ulValueLen = (CK_ULONG)-1; - ret = CKR_BUFFER_TOO_SMALL; - } - - return ret; -} - -CK_RV -mock_C_GetAttributeValue__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_GetAttributeValue__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_GetAttributeValue__fail_first (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count) -{ - return CKR_FUNCTION_REJECTED; -} - -CK_RV -mock_C_GetAttributeValue__fail_late (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count) -{ - CK_ULONG i; - - for (i = 0; i < count; i++) { - if (template[i].pValue) - return CKR_FUNCTION_FAILED; - } - return mock_C_GetAttributeValue (session, object, template, count); -} - -CK_RV -mock_C_SetAttributeValue (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count) -{ - Session *sess; - CK_ATTRIBUTE *attrs; - p11_dict *table; - CK_RV rv; - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - rv = lookup_object (sess, object, &attrs, &table); - if (rv != CKR_OK) - return rv; - - p11_dict_steal (table, handle_to_pointer (object), NULL, (void **)&attrs); - attrs = p11_attrs_buildn (attrs, template, count); - p11_dict_set (table, handle_to_pointer (object), attrs); - return CKR_OK; -} - -CK_RV -mock_C_SetAttributeValue__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_SetAttributeValue__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -typedef struct _FindObjects { - CK_ATTRIBUTE *template; - CK_ULONG count; - Session *sess; -} FindObjects; - -static bool -enumerate_and_find_objects (CK_OBJECT_HANDLE object, - CK_ATTRIBUTE *attrs, - void *user_data) -{ - FindObjects *ctx = user_data; - CK_ATTRIBUTE *match; - CK_ATTRIBUTE *attr; - CK_BBOOL private; - CK_ULONG i; - - if (!logged_in) { - if (p11_attrs_find_bool (attrs, CKA_PRIVATE, &private) && private) - return 1; /* Continue */ - } - - for (i = 0; i < ctx->count; ++i) { - match = ctx->template + i; - attr = p11_attrs_find (attrs, match->type); - if (!attr) - return true; /* Continue */ - - if (attr->ulValueLen != match->ulValueLen || - memcmp (attr->pValue, match->pValue, attr->ulValueLen) != 0) - return true; /* Continue */ - } - - p11_array_push (ctx->sess->matches, handle_to_pointer (object)); - return true; /* Continue */ -} - -static int -compar_handles (const void *one, - const void *two) -{ - void **p1 = (void **)one; - void **p2 = (void **)two; - return pointer_to_handle (*p2) - pointer_to_handle (*p1); -} - -CK_RV -mock_C_FindObjectsInit (CK_SESSION_HANDLE session, - CK_ATTRIBUTE_PTR template, - CK_ULONG count) -{ - Session *sess; - FindObjects ctx; - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - /* Starting an operation, cancels any previous one */ - sess->crypto_mechanism = 0; - sess->hash_mechanism = 0; - - sess->finding = true; - p11_array_free (sess->matches); - sess->matches = p11_array_new (NULL); - - ctx.template = template; - ctx.count = count; - ctx.sess = sess; - - mock_module_enumerate_objects (session, enumerate_and_find_objects, &ctx); - qsort (sess->matches->elem, sess->matches->num, sizeof (void *), compar_handles); - return CKR_OK; -} - -CK_RV -mock_C_FindObjectsInit__invalid_handle (CK_SESSION_HANDLE session, - CK_ATTRIBUTE_PTR template, - CK_ULONG count) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_FindObjectsInit__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_ATTRIBUTE_PTR template, - CK_ULONG count) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_FindObjectsInit__fails (CK_SESSION_HANDLE session, - CK_ATTRIBUTE_PTR template, - CK_ULONG count) -{ - return CKR_DEVICE_MEMORY; -} - -CK_RV -mock_C_FindObjects (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE_PTR objects, - CK_ULONG max_object_count, - CK_ULONG_PTR object_count) -{ - Session *sess; - - return_val_if_fail (objects, CKR_ARGUMENTS_BAD); - return_val_if_fail (object_count, CKR_ARGUMENTS_BAD); - return_val_if_fail (max_object_count != 0, CKR_ARGUMENTS_BAD); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (sess == NULL) - return CKR_SESSION_HANDLE_INVALID; - if (!sess->finding) - return CKR_OPERATION_NOT_INITIALIZED; - - *object_count = 0; - while (max_object_count > 0) { - if (sess->matches->num == 0) - break; - *objects = pointer_to_handle (sess->matches->elem[sess->matches->num - 1]); - ++objects; - --max_object_count; - ++(*object_count); - p11_array_remove (sess->matches, sess->matches->num - 1); - } - - return CKR_OK; -} - -CK_RV -mock_C_FindObjects__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE_PTR objects, - CK_ULONG max_count, - CK_ULONG_PTR count) -{ - return_val_if_fail (count, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_FindObjects__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE_PTR objects, - CK_ULONG max_count, - CK_ULONG_PTR count) -{ - return_val_if_fail (count, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_FindObjects__fails (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE_PTR objects, - CK_ULONG max_count, - CK_ULONG_PTR count) -{ - return_val_if_fail (count, CKR_ARGUMENTS_BAD); - - return CKR_DEVICE_REMOVED; -} - -CK_RV -mock_C_FindObjectsFinal (CK_SESSION_HANDLE session) -{ - - Session *sess; - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (sess == NULL) - return CKR_SESSION_HANDLE_INVALID; - if (!sess->finding) - return CKR_OPERATION_NOT_INITIALIZED; - - sess->finding = false; - p11_array_free (sess->matches); - sess->matches = NULL; - - return CKR_OK; -} - -CK_RV -mock_C_FindObjectsFinal__invalid_handle (CK_SESSION_HANDLE session) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_FindObjectsFinal__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_EncryptInit (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - Session *sess; - - return_val_if_fail (mechanism != NULL, CKR_ARGUMENTS_BAD); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - /* Starting an operation, cancels any previous one */ - sess->finding = CK_FALSE; - - if (mechanism->mechanism != CKM_MOCK_CAPITALIZE) - return CKR_MECHANISM_INVALID; - if (key != MOCK_PUBLIC_KEY_CAPITALIZE) - return CKR_KEY_HANDLE_INVALID; - - sess->crypto_method = CKA_ENCRYPT; - sess->crypto_mechanism = CKM_MOCK_CAPITALIZE; - sess->crypto_key = key; - return CKR_OK; -} - -CK_RV -mock_C_EncryptInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_EncryptInit__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_Encrypt (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR encrypted_data, - CK_ULONG_PTR encrypted_data_len) -{ - CK_ULONG last = 0; - CK_RV rv; - rv = mock_C_EncryptUpdate (session, data, data_len, encrypted_data, encrypted_data_len); - if (rv == CKR_OK) - rv = mock_C_EncryptFinal (session, encrypted_data, &last); - return rv; -} - -CK_RV -mock_C_Encrypt__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR encrypted_data, - CK_ULONG_PTR encrypted_data_len) -{ - return_val_if_fail (encrypted_data_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_Encrypt__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR encrypted_data, - CK_ULONG_PTR encrypted_data_len) -{ - return_val_if_fail (encrypted_data_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_EncryptUpdate (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, - CK_BYTE_PTR encrypted_part, - CK_ULONG_PTR encrypted_part_len) -{ - Session *sess; - CK_ULONG i; - - return_val_if_fail (part != NULL, CKR_DATA_INVALID); - return_val_if_fail (encrypted_part_len != NULL, CKR_ARGUMENTS_BAD); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - if (!sess->crypto_mechanism) - return CKR_OPERATION_NOT_INITIALIZED; - if (sess->crypto_method != CKA_ENCRYPT) - return CKR_OPERATION_NOT_INITIALIZED; - assert (sess->crypto_mechanism == CKM_MOCK_CAPITALIZE); - assert (sess->crypto_key == MOCK_PUBLIC_KEY_CAPITALIZE); - - if (!encrypted_part) { - *encrypted_part_len = part_len; - return CKR_OK; - } - - if (*encrypted_part_len < part_len) { - *encrypted_part_len = part_len; - return CKR_BUFFER_TOO_SMALL; - } - - for (i = 0; i < part_len; ++i) - encrypted_part[i] = toupper (part[i]); - *encrypted_part_len = part_len; - return CKR_OK; -} - -CK_RV -mock_C_EncryptUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, - CK_BYTE_PTR encrypted_part, - CK_ULONG_PTR encrypted_part_len) -{ - return_val_if_fail (encrypted_part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_EncryptUpdate__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, - CK_BYTE_PTR encrypted_part, - CK_ULONG_PTR encrypted_part_len) -{ - return_val_if_fail (encrypted_part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_EncryptFinal (CK_SESSION_HANDLE session, - CK_BYTE_PTR last_encrypted_part, - CK_ULONG_PTR last_encrypted_part_len) -{ - Session *sess; - - return_val_if_fail (last_encrypted_part_len != NULL, CKR_ARGUMENTS_BAD); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - if (!sess->crypto_mechanism) - return CKR_OPERATION_NOT_INITIALIZED; - if (sess->crypto_method != CKA_ENCRYPT) - return CKR_OPERATION_NOT_INITIALIZED; - - *last_encrypted_part_len = 0; - - sess->crypto_method = 0; - sess->crypto_mechanism = 0; - sess->crypto_key = 0; - return CKR_OK; -} - -CK_RV -mock_C_EncryptFinal__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR last_part, - CK_ULONG_PTR last_part_len) -{ - return_val_if_fail (last_part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_EncryptFinal__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR last_part, - CK_ULONG_PTR last_part_len) -{ - return_val_if_fail (last_part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_DecryptInit (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - Session *sess; - - return_val_if_fail (mechanism != NULL, CKR_ARGUMENTS_BAD); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - /* Starting an operation, cancels any previous one */ - sess->finding = false; - - if (mechanism->mechanism != CKM_MOCK_CAPITALIZE) - return CKR_MECHANISM_INVALID; - if (key != MOCK_PRIVATE_KEY_CAPITALIZE) - return CKR_KEY_HANDLE_INVALID; - - sess->crypto_method = CKA_DECRYPT; - sess->crypto_mechanism = CKM_MOCK_CAPITALIZE; - sess->crypto_key = key; - return CKR_OK; -} - -CK_RV -mock_C_DecryptInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_DecryptInit__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_Decrypt (CK_SESSION_HANDLE session, - CK_BYTE_PTR encrypted_data, - CK_ULONG encrypted_data_len, - CK_BYTE_PTR data, - CK_ULONG_PTR data_len) -{ - CK_ULONG last = 0; - CK_RV rv; - rv = mock_C_DecryptUpdate (session, encrypted_data, encrypted_data_len, data, data_len); - if (rv == CKR_OK) - rv = mock_C_DecryptFinal (session, data, &last); - return rv; -} - -CK_RV -mock_C_Decrypt__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_data, - CK_ULONG enc_data_len, - CK_BYTE_PTR data, - CK_ULONG_PTR data_len) -{ - return_val_if_fail (data_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_Decrypt__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_data, - CK_ULONG enc_data_len, - CK_BYTE_PTR data, - CK_ULONG_PTR data_len) -{ - return_val_if_fail (data_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_DecryptUpdate (CK_SESSION_HANDLE session, - CK_BYTE_PTR encrypted_part, - CK_ULONG encrypted_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len) -{ - Session *sess; - CK_ULONG i; - - return_val_if_fail (encrypted_part, CKR_ENCRYPTED_DATA_INVALID); - return_val_if_fail (part_len, CKR_ARGUMENTS_BAD); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - if (!sess->crypto_mechanism) - return CKR_OPERATION_NOT_INITIALIZED; - if (sess->crypto_method != CKA_DECRYPT) - return CKR_OPERATION_NOT_INITIALIZED; - assert (sess->crypto_mechanism == CKM_MOCK_CAPITALIZE); - assert (sess->crypto_key == MOCK_PRIVATE_KEY_CAPITALIZE); - - if (!part) { - *part_len = encrypted_part_len; - return CKR_OK; - } - - if (*part_len < encrypted_part_len) { - *part_len = encrypted_part_len; - return CKR_BUFFER_TOO_SMALL; - } - - for (i = 0; i < encrypted_part_len; ++i) - part[i] = tolower (encrypted_part[i]); - *part_len = encrypted_part_len; - return CKR_OK; -} - -CK_RV -mock_C_DecryptUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_part, - CK_ULONG enc_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len) -{ - return_val_if_fail (part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_DecryptUpdate__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_part, - CK_ULONG enc_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len) -{ - return_val_if_fail (part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_DecryptFinal (CK_SESSION_HANDLE session, - CK_BYTE_PTR last_part, - CK_ULONG_PTR last_part_len) -{ - Session *sess; - - return_val_if_fail (last_part_len != NULL, CKR_ARGUMENTS_BAD); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - if (!sess->crypto_mechanism) - return CKR_OPERATION_NOT_INITIALIZED; - if (sess->crypto_method != CKA_DECRYPT) - return CKR_OPERATION_NOT_INITIALIZED; - - *last_part_len = 0; - - sess->crypto_method = 0; - sess->crypto_mechanism = 0; - sess->crypto_key = 0; - - return CKR_OK; -} - -CK_RV -mock_C_DecryptFinal__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR last_part, - CK_ULONG_PTR last_part_len) -{ - return_val_if_fail (last_part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_DecryptFinal__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR last_part, - CK_ULONG_PTR last_part_len) -{ - return_val_if_fail (last_part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_DigestInit (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism) -{ - Session *sess; - - return_val_if_fail (mechanism != NULL, CKR_ARGUMENTS_BAD); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - /* Starting an operation, cancels any previous one */ - sess->finding = false; - - if (mechanism->mechanism != CKM_MOCK_COUNT) - return CKR_MECHANISM_INVALID; - - sess->hash_mechanism = CKM_MOCK_COUNT; - sess->hash_method = (CK_ULONG)-1; - sess->hash_count = 0; - sess->hash_key = 0; - return CKR_OK; -} - -CK_RV -mock_C_DigestInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_DigestInit__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_Digest (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR digest, - CK_ULONG_PTR digest_len) -{ - CK_RV rv; - - return_val_if_fail (digest_len, CKR_ARGUMENTS_BAD); - - rv = mock_C_DigestUpdate (session, data, data_len); - if (rv == CKR_OK) - rv = mock_C_DigestFinal (session, digest, digest_len); - return rv; -} - -CK_RV -mock_C_Digest__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR digest, - CK_ULONG_PTR digest_len) -{ - return_val_if_fail (digest_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_Digest__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR digest, - CK_ULONG_PTR digest_len) -{ - return_val_if_fail (digest_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_DigestUpdate (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len) -{ - Session *sess; - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - if (!sess->hash_mechanism) - return CKR_OPERATION_NOT_INITIALIZED; - if (sess->hash_method != (CK_ULONG)-1) - return CKR_OPERATION_NOT_INITIALIZED; - assert (sess->hash_mechanism == CKM_MOCK_COUNT); - - sess->hash_count += part_len; - return CKR_OK; -} - -CK_RV -mock_C_DigestUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_DigestUpdate__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_DigestKey (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE key) -{ - Session *sess; - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - if (!sess->hash_mechanism) - return CKR_OPERATION_NOT_INITIALIZED; - if (sess->hash_method != (CK_ULONG)-1) - return CKR_OPERATION_NOT_INITIALIZED; - assert (sess->hash_mechanism == CKM_MOCK_COUNT); - - sess->hash_count += key; - return CKR_OK; -} - -CK_RV -mock_C_DigestKey__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_DigestKey__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_DigestFinal (CK_SESSION_HANDLE session, - CK_BYTE_PTR digest, - CK_ULONG_PTR digest_len) -{ - char buffer[32]; - Session *sess; - int len; - - return_val_if_fail (digest_len != NULL, CKR_ARGUMENTS_BAD); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - if (!sess->hash_mechanism) - return CKR_OPERATION_NOT_INITIALIZED; - if (sess->hash_method != (CK_ULONG)-1) - return CKR_OPERATION_NOT_INITIALIZED; - assert (sess->hash_mechanism == CKM_MOCK_COUNT); - - len = snprintf (buffer, sizeof (buffer), "%lu", sess->hash_count); - - if (!digest) { - *digest_len = len; - return CKR_OK; - } else if (*digest_len < len) { - *digest_len = len; - return CKR_BUFFER_TOO_SMALL; - } - - memcpy (digest, &buffer, len); - *digest_len = len; - - sess->hash_count = 0; - sess->hash_mechanism = 0; - sess->hash_key = 0; - sess->hash_method = 0; - - return CKR_OK; -} - -CK_RV -mock_C_DigestFinal__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR digest, - CK_ULONG_PTR digest_len) -{ - return_val_if_fail (digest_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_DigestFinal__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR digest, - CK_ULONG_PTR digest_len) -{ - return_val_if_fail (digest_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -static CK_RV -prefix_mechanism_init (CK_SESSION_HANDLE session, - CK_ATTRIBUTE_TYPE method, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - Session *sess; - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE *value; - CK_BYTE_PTR param; - CK_ULONG n_param; - CK_ULONG length; - CK_RV rv; - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - if (mechanism->mechanism != CKM_MOCK_PREFIX) - return CKR_MECHANISM_INVALID; - if (method == CKA_SIGN || method == CKA_SIGN_RECOVER) { - if (key != MOCK_PRIVATE_KEY_PREFIX) - return CKR_KEY_HANDLE_INVALID; - } else if (method == CKA_VERIFY || method == CKA_VERIFY_RECOVER) { - if (key != MOCK_PUBLIC_KEY_PREFIX) - return CKR_KEY_HANDLE_INVALID; - } else { - assert_not_reached (); - } - - rv = lookup_object (sess, key, &attrs, NULL); - if (rv != CKR_OK) - return rv; - - value = p11_attrs_find_valid (attrs, CKA_VALUE); - if (value == NULL) - return CKR_KEY_TYPE_INCONSISTENT; - - if (mechanism->pParameter) { - param = mechanism->pParameter; - n_param = mechanism->ulParameterLen; - } else { - param = (CK_BYTE_PTR)SIGNED_PREFIX; - n_param = strlen (SIGNED_PREFIX) + 1; - } - - length = value->ulValueLen + n_param; - if (length > sizeof (sess->sign_prefix)) - return CKR_KEY_SIZE_RANGE; - - /* Starting an operation, cancels any finding */ - sess->finding = false; - - sess->hash_mechanism = CKM_MOCK_PREFIX; - sess->hash_method = method; - sess->hash_key = key; - sess->hash_count = 0; - - memcpy (sess->sign_prefix, param, n_param); - memcpy (sess->sign_prefix + n_param, value->pValue, value->ulValueLen); - sess->n_sign_prefix = length; - - /* The private key has CKA_ALWAYS_AUTHENTICATE above */ - if (method == CKA_SIGN || method == CKA_SIGN_RECOVER) - sess->want_context_login = true; - - return CKR_OK; - -} - -CK_RV -mock_C_SignInit (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - return_val_if_fail (mechanism != NULL, CKR_ARGUMENTS_BAD); - return prefix_mechanism_init (session, CKA_SIGN, mechanism, key); -} - -CK_RV -mock_C_SignInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_SignInit__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_Sign (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len) -{ - CK_RV rv; - - rv = mock_C_SignUpdate (session, data, data_len); - if (rv == CKR_OK) - rv = mock_C_SignFinal (session, signature, signature_len); - - return rv; -} - -CK_RV -mock_C_Sign__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len) -{ - return_val_if_fail (signature_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_Sign__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len) -{ - return_val_if_fail (signature_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_SignUpdate (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len) -{ - Session *sess; - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - if (sess->hash_mechanism != CKM_MOCK_PREFIX || - sess->hash_method != CKA_SIGN) - return CKR_OPERATION_NOT_INITIALIZED; - if (sess->want_context_login) - return CKR_USER_NOT_LOGGED_IN; - - sess->hash_count += part_len; - return CKR_OK; -} - -CK_RV -mock_C_SignUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len) -{ - return_val_if_fail (part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_SignUpdate__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len) -{ - return_val_if_fail (part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_SignFinal (CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len) -{ - char buffer[32]; - Session *sess; - CK_ULONG length; - int len; - - return_val_if_fail (signature_len, CKR_ARGUMENTS_BAD); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - if (sess->hash_mechanism != CKM_MOCK_PREFIX || - sess->hash_method != CKA_SIGN) - return CKR_OPERATION_NOT_INITIALIZED; - if (sess->want_context_login) - return CKR_USER_NOT_LOGGED_IN; - - len = snprintf (buffer, sizeof (buffer), "%lu", sess->hash_count); - length = sess->n_sign_prefix + len; - - if (!signature) { - *signature_len = length; - return CKR_OK; - } - - if (*signature_len < length) { - *signature_len = length; - return CKR_BUFFER_TOO_SMALL; - } - - memcpy (signature, sess->sign_prefix, sess->n_sign_prefix); - memcpy (signature + sess->n_sign_prefix, buffer, len); - *signature_len = length; - - sess->hash_mechanism = 0; - sess->hash_method = 0; - sess->hash_count = 0; - sess->hash_key = 0; - - return CKR_OK; -} - -CK_RV -mock_C_SignFinal__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len) -{ - return_val_if_fail (signature_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_SignFinal__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len) -{ - return_val_if_fail (signature_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_SignRecoverInit (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - return_val_if_fail (mechanism != NULL, CKR_ARGUMENTS_BAD); - return prefix_mechanism_init (session, CKA_SIGN_RECOVER, mechanism, key); -} - -CK_RV -mock_C_SignRecoverInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_SignRecoverInit__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_SignRecover (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len) -{ - Session *sess; - CK_ULONG length; - - return_val_if_fail (data, CKR_DATA_INVALID); - return_val_if_fail (signature_len, CKR_ARGUMENTS_BAD); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - if (sess->hash_method != CKA_SIGN_RECOVER || - sess->hash_mechanism != CKM_MOCK_PREFIX) - return CKR_OPERATION_NOT_INITIALIZED; - if (sess->want_context_login) - return CKR_USER_NOT_LOGGED_IN; - - length = sess->n_sign_prefix + data_len; - - if (!signature) { - *signature_len = length; - return CKR_OK; - } - - if (*signature_len < length) { - *signature_len = length; - return CKR_BUFFER_TOO_SMALL; - } - - memcpy (signature, sess->sign_prefix, sess->n_sign_prefix); - memcpy (signature + sess->n_sign_prefix, data, data_len); - *signature_len = length; - - sess->hash_method = 0; - sess->hash_mechanism = 0; - sess->hash_key = 0; - sess->hash_count = 0; - - return CKR_OK; -} - -CK_RV -mock_C_SignRecover__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len) -{ - return_val_if_fail (signature_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_SignRecover__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len) -{ - return_val_if_fail (signature_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_VerifyInit (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - return_val_if_fail (mechanism != NULL, CKR_ARGUMENTS_BAD); - return prefix_mechanism_init (session, CKA_VERIFY, mechanism, key); -} - -CK_RV -mock_C_VerifyInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_VerifyInit__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_Verify (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG signature_len) -{ - CK_RV rv; - - rv = mock_C_VerifyUpdate (session, data, data_len); - if (rv == CKR_OK) - rv = mock_C_VerifyFinal (session, signature, signature_len); - - return rv; -} - -CK_RV -mock_C_Verify__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG signature_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_Verify__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG signature_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_VerifyUpdate (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len) -{ - Session *sess; - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - if (sess->hash_mechanism != CKM_MOCK_PREFIX || - sess->hash_method != CKA_VERIFY) - return CKR_OPERATION_NOT_INITIALIZED; - if (sess->want_context_login) - return CKR_USER_NOT_LOGGED_IN; - - sess->hash_count += part_len; - return CKR_OK; -} - -CK_RV -mock_C_VerifyUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_VerifyUpdate__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_VerifyFinal (CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG signature_len) -{ - char buffer[32]; - Session *sess; - CK_ULONG length; - int len; - - return_val_if_fail (signature, CKR_ARGUMENTS_BAD); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - if (sess->hash_mechanism != CKM_MOCK_PREFIX || - sess->hash_method != CKA_VERIFY) - return CKR_OPERATION_NOT_INITIALIZED; - if (sess->want_context_login) - return CKR_USER_NOT_LOGGED_IN; - - len = snprintf (buffer, sizeof (buffer), "%lu", sess->hash_count); - length = sess->n_sign_prefix + len; - - if (signature_len != length) - return CKR_SIGNATURE_LEN_RANGE; - - if (memcmp (signature, sess->sign_prefix, sess->n_sign_prefix) != 0 || - memcmp (signature + sess->n_sign_prefix, buffer, len) != 0) - return CKR_SIGNATURE_INVALID; - - sess->hash_mechanism = 0; - sess->hash_method = 0; - sess->hash_count = 0; - sess->hash_key = 0; - - return CKR_OK; -} - -CK_RV -mock_C_VerifyFinal__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG signature_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_VerifyFinal__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG signature_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_VerifyRecoverInit (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - return_val_if_fail (mechanism != NULL, CKR_ARGUMENTS_BAD); - return prefix_mechanism_init (session, CKA_VERIFY_RECOVER, mechanism, key); -} - -CK_RV -mock_C_VerifyRecoverInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_VerifyRecoverInit__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_VerifyRecover (CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG signature_len, - CK_BYTE_PTR data, - CK_ULONG_PTR data_len) -{ - Session *sess; - CK_ULONG length; - - return_val_if_fail (signature, CKR_ARGUMENTS_BAD); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - if (sess->hash_mechanism != CKM_MOCK_PREFIX || - sess->hash_method != CKA_VERIFY_RECOVER) - return CKR_OPERATION_NOT_INITIALIZED; - if (sess->want_context_login) - return CKR_USER_NOT_LOGGED_IN; - - if (signature_len < sess->n_sign_prefix) - return CKR_SIGNATURE_LEN_RANGE; - if (memcmp (signature, sess->sign_prefix, sess->n_sign_prefix) != 0) - return CKR_SIGNATURE_INVALID; - - length = signature_len - sess->n_sign_prefix; - if (!data) { - *data_len = length; - return CKR_OK; - } - - if (*data_len < length) { - *data_len = length; - return CKR_BUFFER_TOO_SMALL; - } - - *data_len = length; - memcpy (data, signature + sess->n_sign_prefix, length); - return CKR_OK; -} - -CK_RV -mock_C_VerifyRecover__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG signature_len, - CK_BYTE_PTR data, - CK_ULONG_PTR data_len) -{ - return_val_if_fail (data_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_VerifyRecover__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG signature_len, - CK_BYTE_PTR data, - CK_ULONG_PTR data_len) -{ - return_val_if_fail (data_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_DigestEncryptUpdate (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, - CK_BYTE_PTR encrypted_part, - CK_ULONG_PTR encrypted_part_len) -{ - CK_RV rv; - - rv = mock_C_EncryptUpdate (session, part, part_len, encrypted_part, encrypted_part_len); - if (rv == CKR_OK) - rv = mock_C_DigestUpdate (session, part, part_len); - - return rv; -} - -CK_RV -mock_C_DigestEncryptUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, - CK_BYTE_PTR enc_part, - CK_ULONG_PTR enc_part_len) -{ - return_val_if_fail (enc_part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_DigestEncryptUpdate__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, - CK_BYTE_PTR enc_part, - CK_ULONG_PTR enc_part_len) -{ - return_val_if_fail (enc_part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_DecryptDigestUpdate (CK_SESSION_HANDLE session, - CK_BYTE_PTR encrypted_part, - CK_ULONG encrypted_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len) -{ - CK_RV rv; - - rv = mock_C_DecryptUpdate (session, encrypted_part, encrypted_part_len, part, part_len); - if (rv == CKR_OK) - rv = mock_C_DigestUpdate (session, part, *part_len); - - return rv; -} - -CK_RV -mock_C_DecryptDigestUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_part, - CK_ULONG enc_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len) -{ - return_val_if_fail (part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_DecryptDigestUpdate__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_part, - CK_ULONG enc_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len) -{ - return_val_if_fail (part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_SignEncryptUpdate (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, - CK_BYTE_PTR encrypted_part, - CK_ULONG_PTR encrypted_part_len) -{ - CK_RV rv; - - rv = mock_C_EncryptUpdate (session, part, part_len, encrypted_part, encrypted_part_len); - if (rv == CKR_OK) - rv = mock_C_SignUpdate (session, part, part_len); - - return rv; -} - -CK_RV -mock_C_SignEncryptUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, - CK_BYTE_PTR enc_part, - CK_ULONG_PTR enc_part_len) -{ - return_val_if_fail (enc_part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_SignEncryptUpdate__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, - CK_BYTE_PTR enc_part, - CK_ULONG_PTR enc_part_len) -{ - return_val_if_fail (enc_part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_DecryptVerifyUpdate (CK_SESSION_HANDLE session, - CK_BYTE_PTR encrypted_part, - CK_ULONG encrypted_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len) -{ - CK_RV rv; - - rv = mock_C_DecryptUpdate (session, encrypted_part, encrypted_part_len, part, part_len); - if (rv == CKR_OK) - rv = mock_C_VerifyUpdate (session, part, *part_len); - - return rv; -} - -CK_RV -mock_C_DecryptVerifyUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_part, - CK_ULONG enc_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len) -{ - return_val_if_fail (part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_DecryptVerifyUpdate__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_part, - CK_ULONG enc_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len) -{ - return_val_if_fail (part_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_GenerateKey (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key) -{ - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE value; - Session *sess; - CK_BBOOL token; - - return_val_if_fail (mechanism, CKR_MECHANISM_INVALID); - return_val_if_fail (template, CKR_TEMPLATE_INCOMPLETE); - return_val_if_fail (count, CKR_TEMPLATE_INCOMPLETE); - return_val_if_fail (key, CKR_ARGUMENTS_BAD); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - if (mechanism->mechanism != CKM_MOCK_GENERATE) - return CKR_MECHANISM_INVALID; - - if (!mechanism->pParameter || mechanism->ulParameterLen != 9 || - memcmp (mechanism->pParameter, "generate", 9) != 0) - return CKR_MECHANISM_PARAM_INVALID; - - value.type = CKA_VALUE; - value.pValue = "generated"; - value.ulValueLen = strlen (value.pValue); - - attrs = p11_attrs_buildn (NULL, template, count); - attrs = p11_attrs_buildn (attrs, &value, 1); - - *key = ++unique_identifier; - if (p11_attrs_find_bool (attrs, CKA_TOKEN, &token) && token) - p11_dict_set (the_objects, handle_to_pointer (*key), attrs); - else - p11_dict_set (sess->objects, handle_to_pointer (*key), attrs); - - return CKR_OK; -} - -CK_RV -mock_C_GenerateKey__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_GenerateKey__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_GenerateKeyPair (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_ATTRIBUTE_PTR public_key_template, - CK_ULONG public_key_count, - CK_ATTRIBUTE_PTR private_key_template, - CK_ULONG private_key_count, - CK_OBJECT_HANDLE_PTR public_key, - CK_OBJECT_HANDLE_PTR private_key) -{ - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE value; - Session *sess; - CK_BBOOL token; - - return_val_if_fail (mechanism, CKR_MECHANISM_INVALID); - return_val_if_fail (public_key_template, CKR_TEMPLATE_INCOMPLETE); - return_val_if_fail (public_key_count, CKR_TEMPLATE_INCOMPLETE); - return_val_if_fail (private_key_template, CKR_TEMPLATE_INCOMPLETE); - return_val_if_fail (private_key_count, CKR_TEMPLATE_INCOMPLETE); - return_val_if_fail (public_key, CKR_ARGUMENTS_BAD); - return_val_if_fail (private_key, CKR_ARGUMENTS_BAD); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - if (mechanism->mechanism != CKM_MOCK_GENERATE) - return CKR_MECHANISM_INVALID; - - if (!mechanism->pParameter || mechanism->ulParameterLen != 9 || - memcmp (mechanism->pParameter, "generate", 9) != 0) - return CKR_MECHANISM_PARAM_INVALID; - - value.type = CKA_VALUE; - value.pValue = "generated"; - value.ulValueLen = strlen (value.pValue); - - attrs = p11_attrs_buildn (NULL, public_key_template, public_key_count); - attrs = p11_attrs_buildn (attrs, &value, 1); - - *public_key = ++unique_identifier; - if (p11_attrs_find_bool (attrs, CKA_TOKEN, &token) && token) - p11_dict_set (the_objects, handle_to_pointer (*public_key), attrs); - else - p11_dict_set (sess->objects, handle_to_pointer (*public_key), attrs); - - attrs = p11_attrs_buildn (NULL, private_key_template, private_key_count); - attrs = p11_attrs_buildn (attrs, &value, 1); - - *private_key = ++unique_identifier; - if (p11_attrs_find_bool (attrs, CKA_TOKEN, &token) && token) - p11_dict_set (the_objects, handle_to_pointer (*private_key), attrs); - else - p11_dict_set (sess->objects, handle_to_pointer (*private_key), attrs); - - return CKR_OK; -} - -CK_RV -mock_C_GenerateKeyPair__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_ATTRIBUTE_PTR pub_template, - CK_ULONG pub_count, - CK_ATTRIBUTE_PTR priv_template, - CK_ULONG priv_count, - CK_OBJECT_HANDLE_PTR pub_key, - CK_OBJECT_HANDLE_PTR priv_key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_GenerateKeyPair__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_ATTRIBUTE_PTR pub_template, - CK_ULONG pub_count, - CK_ATTRIBUTE_PTR priv_template, - CK_ULONG priv_count, - CK_OBJECT_HANDLE_PTR pub_key, - CK_OBJECT_HANDLE_PTR priv_key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_WrapKey (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE wrapping_key, - CK_OBJECT_HANDLE key, - CK_BYTE_PTR wrapped_key, - CK_ULONG_PTR wrapped_key_len) -{ - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE *attr; - Session *sess; - CK_RV rv; - - return_val_if_fail (mechanism, CKR_MECHANISM_INVALID); - return_val_if_fail (wrapping_key, CKR_OBJECT_HANDLE_INVALID); - return_val_if_fail (key, CKR_OBJECT_HANDLE_INVALID); - return_val_if_fail (wrapped_key_len, CKR_WRAPPED_KEY_LEN_RANGE); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - rv = lookup_object (sess, wrapping_key, &attrs, NULL); - if (rv == CKR_OBJECT_HANDLE_INVALID) - return CKR_WRAPPING_KEY_HANDLE_INVALID; - else if (rv != CKR_OK) - return rv; - - rv = lookup_object (sess, key, &attrs, NULL); - if (rv == CKR_OBJECT_HANDLE_INVALID) - return CKR_WRAPPING_KEY_HANDLE_INVALID; - else if (rv != CKR_OK) - return rv; - - if (mechanism->mechanism != CKM_MOCK_WRAP) - return CKR_MECHANISM_INVALID; - - if (mechanism->pParameter == NULL || - mechanism->ulParameterLen != 4 || - memcmp (mechanism->pParameter, "wrap", 4) != 0) { - return CKR_MECHANISM_PARAM_INVALID; - } - - attr = p11_attrs_find_valid (attrs, CKA_VALUE); - if (attr == NULL) - return CKR_WRAPPED_KEY_INVALID; - - if (!wrapped_key) { - *wrapped_key_len = attr->ulValueLen; - return CKR_OK; - } - - if (*wrapped_key_len < attr->ulValueLen) { - *wrapped_key_len = attr->ulValueLen; - return CKR_BUFFER_TOO_SMALL; - } - - memcpy (wrapped_key, attr->pValue, attr->ulValueLen); - *wrapped_key_len = attr->ulValueLen; - - return CKR_OK; -} - -CK_RV -mock_C_WrapKey__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE wrapping_key, - CK_OBJECT_HANDLE key, - CK_BYTE_PTR wrapped_key, - CK_ULONG_PTR wrapped_key_len) -{ - return_val_if_fail (wrapped_key_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_WrapKey__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE wrapping_key, - CK_OBJECT_HANDLE key, - CK_BYTE_PTR wrapped_key, - CK_ULONG_PTR wrapped_key_len) -{ - return_val_if_fail (wrapped_key_len, CKR_ARGUMENTS_BAD); - - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_UnwrapKey (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE unwrapping_key, - CK_BYTE_PTR wrapped_key, - CK_ULONG wrapped_key_len, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key) -{ - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE value; - Session *sess; - CK_BBOOL token; - CK_RV rv; - - return_val_if_fail (mechanism, CKR_MECHANISM_INVALID); - return_val_if_fail (unwrapping_key, CKR_WRAPPING_KEY_HANDLE_INVALID); - return_val_if_fail (wrapped_key, CKR_WRAPPED_KEY_INVALID); - return_val_if_fail (wrapped_key_len, CKR_WRAPPED_KEY_LEN_RANGE); - return_val_if_fail (key, CKR_ARGUMENTS_BAD); - return_val_if_fail (template, CKR_TEMPLATE_INCOMPLETE); - return_val_if_fail (count, CKR_TEMPLATE_INCONSISTENT); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - rv = lookup_object (sess, unwrapping_key, &attrs, NULL); - if (rv == CKR_OBJECT_HANDLE_INVALID) - return CKR_WRAPPING_KEY_HANDLE_INVALID; - else if (rv != CKR_OK) - return rv; - - if (mechanism->mechanism != CKM_MOCK_WRAP) - return CKR_MECHANISM_INVALID; - - if (mechanism->pParameter == NULL || - mechanism->ulParameterLen != 4 || - memcmp (mechanism->pParameter, "wrap", 4) != 0) { - return CKR_MECHANISM_PARAM_INVALID; - } - - value.type = CKA_VALUE; - value.pValue = wrapped_key; - value.ulValueLen = wrapped_key_len; - - attrs = p11_attrs_buildn (NULL, template, count); - attrs = p11_attrs_buildn (attrs, &value, 1); - - *key = ++unique_identifier; - if (p11_attrs_find_bool (attrs, CKA_TOKEN, &token) && token) - p11_dict_set (the_objects, handle_to_pointer (*key), attrs); - else - p11_dict_set (sess->objects, handle_to_pointer (*key), attrs); - - return CKR_OK; -} - -CK_RV -mock_C_UnwrapKey__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE unwrapping_key, - CK_BYTE_PTR wrapped_key, - CK_ULONG wrapped_key_len, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_UnwrapKey__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE unwrapping_key, - CK_BYTE_PTR wrapped_key, - CK_ULONG wrapped_key_len, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_DeriveKey (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE base_key, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key) -{ - CK_ATTRIBUTE *attrs, *copy; - CK_ATTRIBUTE value; - Session *sess; - CK_BBOOL token; - CK_RV rv; - - return_val_if_fail (mechanism, CKR_MECHANISM_INVALID); - return_val_if_fail (count, CKR_TEMPLATE_INCOMPLETE); - return_val_if_fail (template, CKR_TEMPLATE_INCOMPLETE); - return_val_if_fail (key, CKR_ARGUMENTS_BAD); - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - rv = lookup_object (sess, base_key, &attrs, NULL); - if (rv == CKR_OBJECT_HANDLE_INVALID) - return CKR_KEY_HANDLE_INVALID; - else if (rv != CKR_OK) - return rv; - - if (mechanism->mechanism != CKM_MOCK_DERIVE) - return CKR_MECHANISM_INVALID; - - if (mechanism->pParameter == NULL || - mechanism->ulParameterLen != 6 || - memcmp (mechanism->pParameter, "derive", 6) != 0) { - return CKR_MECHANISM_PARAM_INVALID; - } - - value.type = CKA_VALUE; - value.pValue = "derived"; - value.ulValueLen = strlen (value.pValue); - - copy = p11_attrs_buildn (NULL, template, count); - copy = p11_attrs_buildn (copy, &value, 1); - - *key = ++unique_identifier; - if (p11_attrs_find_bool (copy, CKA_TOKEN, &token) && token) - p11_dict_set (the_objects, handle_to_pointer (*key), copy); - else - p11_dict_set (sess->objects, handle_to_pointer (*key), copy); - - return CKR_OK; -} - -CK_RV -mock_C_DeriveKey__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE base_key, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_DeriveKey__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE base_key, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_SeedRandom (CK_SESSION_HANDLE session, - CK_BYTE_PTR seed, - CK_ULONG seed_len) -{ - Session *sess; - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - if (seed_len > sizeof (sess->random_seed)) - return CKR_RANDOM_SEED_NOT_SUPPORTED; - - memcpy (sess->random_seed, seed, seed_len); - sess->random_seed_len = seed_len; - return CKR_OK; -} - -CK_RV -mock_C_SeedRandom__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR seed, - CK_ULONG seed_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_SeedRandom__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR seed, - CK_ULONG seed_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_C_GenerateRandom (CK_SESSION_HANDLE session, - CK_BYTE_PTR random_data, - CK_ULONG random_len) -{ - Session *sess; - CK_ULONG block; - - sess = p11_dict_get (the_sessions, handle_to_pointer (session)); - if (!sess) - return CKR_SESSION_HANDLE_INVALID; - - while (random_len > 0) { - block = sess->random_seed_len; - if (block > random_len) - block = random_len; - memcpy (random_data, sess->random_seed, block); - random_data += block; - random_len -= block; - } - - return CKR_OK; -} - -CK_RV -mock_C_GenerateRandom__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR random_data, - CK_ULONG random_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_RV -mock_X_GenerateRandom__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR random_data, - CK_ULONG random_len) -{ - return CKR_SESSION_HANDLE_INVALID; -} - -CK_FUNCTION_LIST mock_module_no_slots = { - { CRYPTOKI_VERSION_MAJOR, CRYPTOKI_VERSION_MINOR }, /* version */ - mock_C_Initialize, - mock_C_Finalize, - mock_C_GetInfo, - mock_C_GetFunctionList_not_supported, - mock_C_GetSlotList__no_tokens, - mock_C_GetSlotInfo__invalid_slotid, - mock_C_GetTokenInfo__invalid_slotid, - mock_C_GetMechanismList__invalid_slotid, - mock_C_GetMechanismInfo__invalid_slotid, - mock_C_InitToken__invalid_slotid, - mock_C_InitPIN__invalid_handle, - mock_C_SetPIN__invalid_handle, - mock_C_OpenSession__invalid_slotid, - mock_C_CloseSession__invalid_handle, - mock_C_CloseAllSessions__invalid_slotid, - mock_C_GetSessionInfo__invalid_handle, - mock_C_GetOperationState__invalid_handle, - mock_C_SetOperationState__invalid_handle, - mock_C_Login__invalid_handle, - mock_C_Logout__invalid_handle, - mock_C_CreateObject__invalid_handle, - mock_C_CopyObject__invalid_handle, - mock_C_DestroyObject__invalid_handle, - mock_C_GetObjectSize__invalid_handle, - mock_C_GetAttributeValue__invalid_handle, - mock_C_SetAttributeValue__invalid_handle, - mock_C_FindObjectsInit__invalid_handle, - mock_C_FindObjects__invalid_handle, - mock_C_FindObjectsFinal__invalid_handle, - mock_C_EncryptInit__invalid_handle, - mock_C_Encrypt__invalid_handle, - mock_C_EncryptUpdate__invalid_handle, - mock_C_EncryptFinal__invalid_handle, - mock_C_DecryptInit__invalid_handle, - mock_C_Decrypt__invalid_handle, - mock_C_DecryptUpdate__invalid_handle, - mock_C_DecryptFinal__invalid_handle, - mock_C_DigestInit__invalid_handle, - mock_C_Digest__invalid_handle, - mock_C_DigestUpdate__invalid_handle, - mock_C_DigestKey__invalid_handle, - mock_C_DigestFinal__invalid_handle, - mock_C_SignInit__invalid_handle, - mock_C_Sign__invalid_handle, - mock_C_SignUpdate__invalid_handle, - mock_C_SignFinal__invalid_handle, - mock_C_SignRecoverInit__invalid_handle, - mock_C_SignRecover__invalid_handle, - mock_C_VerifyInit__invalid_handle, - mock_C_Verify__invalid_handle, - mock_C_VerifyUpdate__invalid_handle, - mock_C_VerifyFinal__invalid_handle, - mock_C_VerifyRecoverInit__invalid_handle, - mock_C_VerifyRecover__invalid_handle, - mock_C_DigestEncryptUpdate__invalid_handle, - mock_C_DecryptDigestUpdate__invalid_handle, - mock_C_SignEncryptUpdate__invalid_handle, - mock_C_DecryptVerifyUpdate__invalid_handle, - mock_C_GenerateKey__invalid_handle, - mock_C_GenerateKeyPair__invalid_handle, - mock_C_WrapKey__invalid_handle, - mock_C_UnwrapKey__invalid_handle, - mock_C_DeriveKey__invalid_handle, - mock_C_SeedRandom__invalid_handle, - mock_C_GenerateRandom__invalid_handle, - mock_C_GetFunctionStatus__not_parallel, - mock_C_CancelFunction__not_parallel, - mock_C_WaitForSlotEvent__no_event, -}; - -CK_X_FUNCTION_LIST mock_x_module_no_slots = { - { CRYPTOKI_VERSION_MAJOR, CRYPTOKI_VERSION_MINOR }, /* version */ - mock_X_Initialize, - mock_X_Finalize, - mock_X_GetInfo, - mock_X_GetSlotList__no_tokens, - mock_X_GetSlotInfo__invalid_slotid, - mock_X_GetTokenInfo__invalid_slotid, - mock_X_GetMechanismList__invalid_slotid, - mock_X_GetMechanismInfo__invalid_slotid, - mock_X_InitToken__invalid_slotid, - mock_X_InitPIN__invalid_handle, - mock_X_SetPIN__invalid_handle, - mock_X_OpenSession__invalid_slotid, - mock_X_CloseSession__invalid_handle, - mock_X_CloseAllSessions__invalid_slotid, - mock_X_GetSessionInfo__invalid_handle, - mock_X_GetOperationState__invalid_handle, - mock_X_SetOperationState__invalid_handle, - mock_X_Login__invalid_handle, - mock_X_Logout__invalid_handle, - mock_X_CreateObject__invalid_handle, - mock_X_CopyObject__invalid_handle, - mock_X_DestroyObject__invalid_handle, - mock_X_GetObjectSize__invalid_handle, - mock_X_GetAttributeValue__invalid_handle, - mock_X_SetAttributeValue__invalid_handle, - mock_X_FindObjectsInit__invalid_handle, - mock_X_FindObjects__invalid_handle, - mock_X_FindObjectsFinal__invalid_handle, - mock_X_EncryptInit__invalid_handle, - mock_X_Encrypt__invalid_handle, - mock_X_EncryptUpdate__invalid_handle, - mock_X_EncryptFinal__invalid_handle, - mock_X_DecryptInit__invalid_handle, - mock_X_Decrypt__invalid_handle, - mock_X_DecryptUpdate__invalid_handle, - mock_X_DecryptFinal__invalid_handle, - mock_X_DigestInit__invalid_handle, - mock_X_Digest__invalid_handle, - mock_X_DigestUpdate__invalid_handle, - mock_X_DigestKey__invalid_handle, - mock_X_DigestFinal__invalid_handle, - mock_X_SignInit__invalid_handle, - mock_X_Sign__invalid_handle, - mock_X_SignUpdate__invalid_handle, - mock_X_SignFinal__invalid_handle, - mock_X_SignRecoverInit__invalid_handle, - mock_X_SignRecover__invalid_handle, - mock_X_VerifyInit__invalid_handle, - mock_X_Verify__invalid_handle, - mock_X_VerifyUpdate__invalid_handle, - mock_X_VerifyFinal__invalid_handle, - mock_X_VerifyRecoverInit__invalid_handle, - mock_X_VerifyRecover__invalid_handle, - mock_X_DigestEncryptUpdate__invalid_handle, - mock_X_DecryptDigestUpdate__invalid_handle, - mock_X_SignEncryptUpdate__invalid_handle, - mock_X_DecryptVerifyUpdate__invalid_handle, - mock_X_GenerateKey__invalid_handle, - mock_X_GenerateKeyPair__invalid_handle, - mock_X_WrapKey__invalid_handle, - mock_X_UnwrapKey__invalid_handle, - mock_X_DeriveKey__invalid_handle, - mock_X_SeedRandom__invalid_handle, - mock_X_GenerateRandom__invalid_handle, - mock_X_WaitForSlotEvent__no_event, -}; - -CK_FUNCTION_LIST mock_module = { - { CRYPTOKI_VERSION_MAJOR, CRYPTOKI_VERSION_MINOR }, /* version */ - mock_C_Initialize, - mock_C_Finalize, - mock_C_GetInfo, - mock_C_GetFunctionList_not_supported, - mock_C_GetSlotList, - mock_C_GetSlotInfo, - mock_C_GetTokenInfo, - mock_C_GetMechanismList, - mock_C_GetMechanismInfo, - mock_C_InitToken__specific_args, - mock_C_InitPIN__specific_args, - mock_C_SetPIN__specific_args, - mock_C_OpenSession, - mock_C_CloseSession, - mock_C_CloseAllSessions, - mock_C_GetSessionInfo, - mock_C_GetOperationState, - mock_C_SetOperationState, - mock_C_Login, - mock_C_Logout, - mock_C_CreateObject, - mock_C_CopyObject, - mock_C_DestroyObject, - mock_C_GetObjectSize, - mock_C_GetAttributeValue, - mock_C_SetAttributeValue, - mock_C_FindObjectsInit, - mock_C_FindObjects, - mock_C_FindObjectsFinal, - mock_C_EncryptInit, - mock_C_Encrypt, - mock_C_EncryptUpdate, - mock_C_EncryptFinal, - mock_C_DecryptInit, - mock_C_Decrypt, - mock_C_DecryptUpdate, - mock_C_DecryptFinal, - mock_C_DigestInit, - mock_C_Digest, - mock_C_DigestUpdate, - mock_C_DigestKey, - mock_C_DigestFinal, - mock_C_SignInit, - mock_C_Sign, - mock_C_SignUpdate, - mock_C_SignFinal, - mock_C_SignRecoverInit, - mock_C_SignRecover, - mock_C_VerifyInit, - mock_C_Verify, - mock_C_VerifyUpdate, - mock_C_VerifyFinal, - mock_C_VerifyRecoverInit, - mock_C_VerifyRecover, - mock_C_DigestEncryptUpdate, - mock_C_DecryptDigestUpdate, - mock_C_SignEncryptUpdate, - mock_C_DecryptVerifyUpdate, - mock_C_GenerateKey, - mock_C_GenerateKeyPair, - mock_C_WrapKey, - mock_C_UnwrapKey, - mock_C_DeriveKey, - mock_C_SeedRandom, - mock_C_GenerateRandom, - mock_C_GetFunctionStatus, - mock_C_CancelFunction, - mock_C_WaitForSlotEvent, -}; - -void -mock_module_init (void) -{ - static bool initialized = false; - if (!initialized) { - p11_mutex_init (&init_mutex); - initialized = true; - } -} diff --git a/common/mock.h b/common/mock.h deleted file mode 100644 index 16beb66..0000000 --- a/common/mock.h +++ /dev/null @@ -1,1134 +0,0 @@ -/* - * Copyright (c) 2013, 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@redhat.com> - */ - -#ifndef __MOCK_H__ -#define __MOCK_H__ - -#include "compat.h" -#include "pkcs11.h" -#include "pkcs11i.h" - -enum { - MOCK_DATA_OBJECT = 2, - MOCK_PRIVATE_KEY_CAPITALIZE = 3, - MOCK_PUBLIC_KEY_CAPITALIZE = 4, - MOCK_PRIVATE_KEY_PREFIX = 5, - MOCK_PUBLIC_KEY_PREFIX = 6, - - /* - * CKM_MOCK_CAPITALIZE (encrypt/decrypt) - * - Capitalizes to encrypt - * - Lowercase to decrypt - */ - CKM_MOCK_CAPITALIZE = (CKM_VENDOR_DEFINED | 1), - - /* - * CKM_MOCK_PREFIX (sign/verify) - * - Sign prefixes the data with a key label - * - Verify unprefixes data using key label - */ - CKM_MOCK_PREFIX = (CKM_VENDOR_DEFINED | 2), - - /* - * CKM_MOCK_GENERATE (generate-pair) - * - Generates a pair of keys, mechanism parameter should be 'generate' - */ - CKM_MOCK_GENERATE = (CKM_VENDOR_DEFINED | 3), - - /* - * CKM_MOCK_WRAP (wrap key) - * - Wraps key by returning value, mechanism parameter should be 'wrap' - */ - CKM_MOCK_WRAP = (CKM_VENDOR_DEFINED | 4), - - /* - * CKM_MOCK_DERIVE (derive-key) - * - Derives key by setting value to 'derived' - * - Mechanism param should be 'derive' - */ - CKM_MOCK_DERIVE = (CKM_VENDOR_DEFINED | 5), - - /* - * CKM_MOCK_COUNT (digest) - * - Counts the number of bytes, and returns a CK_ULONG 'hash' value - */ - CKM_MOCK_COUNT = (CKM_VENDOR_DEFINED | 6), - - MOCK_SLOT_ONE_ID = 52, - MOCK_SLOT_TWO_ID = 134, - - MOCK_SLOTS_PRESENT = 1, - MOCK_SLOTS_ALL = 2, -}; - -static const CK_INFO MOCK_INFO = { - { CRYPTOKI_VERSION_MAJOR, CRYPTOKI_VERSION_MINOR }, - "MOCK MANUFACTURER ", - 0, - "MOCK LIBRARY ", - { 45, 145 } -}; - -extern CK_FUNCTION_LIST mock_module; - -extern CK_FUNCTION_LIST mock_module_no_slots; - -extern CK_X_FUNCTION_LIST mock_x_module_no_slots; - -void mock_module_init (void); - -typedef bool (* mock_enumerator) (CK_OBJECT_HANDLE handle, - CK_ATTRIBUTE *attrs, - void *user_data); - -void mock_module_enumerate_objects (CK_SESSION_HANDLE session, - mock_enumerator func, - void *user_data); - -void mock_module_add_object (CK_SLOT_ID slot_id, - const CK_ATTRIBUTE *attrs); - -void mock_module_reset (void); - -bool mock_module_initialized (void); - -void mock_module_take_object (CK_SLOT_ID slot_id, - CK_ATTRIBUTE *attrs); - -CK_RV mock_C_Initialize (CK_VOID_PTR init_args); - -CK_RV mock_C_Initialize__fails (CK_VOID_PTR init_args); - -CK_RV mock_X_Initialize (CK_X_FUNCTION_LIST *self, - CK_VOID_PTR init_args); - -CK_RV mock_X_Initialize__fails (CK_X_FUNCTION_LIST *self, - CK_VOID_PTR init_args); - -CK_RV mock_C_Finalize (CK_VOID_PTR reserved); - -CK_RV mock_X_Finalize (CK_X_FUNCTION_LIST *self, - CK_VOID_PTR reserved); - -CK_RV mock_C_GetInfo (CK_INFO_PTR info); - -CK_RV mock_X_GetInfo (CK_X_FUNCTION_LIST *self, - CK_INFO_PTR info); - -CK_RV mock_C_GetFunctionList_not_supported (CK_FUNCTION_LIST_PTR_PTR list); - -CK_RV mock_C_GetSlotList (CK_BBOOL token_present, - CK_SLOT_ID_PTR slot_list, - CK_ULONG_PTR count); - -CK_RV mock_C_GetSlotList__no_tokens (CK_BBOOL token_present, - CK_SLOT_ID_PTR slot_list, - CK_ULONG_PTR count); - -CK_RV mock_C_GetSlotList__fail_first (CK_BBOOL token_present, - CK_SLOT_ID_PTR slot_list, - CK_ULONG_PTR count); - -CK_RV mock_C_GetSlotList__fail_late (CK_BBOOL token_present, - CK_SLOT_ID_PTR slot_list, - CK_ULONG_PTR count); - -CK_RV mock_C_GetSlotInfo (CK_SLOT_ID slot_id, - CK_SLOT_INFO_PTR info); - -CK_RV mock_X_GetSlotList__no_tokens (CK_X_FUNCTION_LIST *self, - CK_BBOOL token_present, - CK_SLOT_ID_PTR slot_list, - CK_ULONG_PTR count); - -CK_RV mock_C_GetSlotInfo__invalid_slotid (CK_SLOT_ID slot_id, - CK_SLOT_INFO_PTR info); - -CK_RV mock_X_GetSlotInfo__invalid_slotid (CK_X_FUNCTION_LIST *self, - CK_SLOT_ID slot_id, - CK_SLOT_INFO_PTR info); - -CK_RV mock_C_GetTokenInfo (CK_SLOT_ID slot_id, - CK_TOKEN_INFO_PTR info); - -CK_RV mock_C_GetTokenInfo__invalid_slotid (CK_SLOT_ID slot_id, - CK_TOKEN_INFO_PTR info); - -CK_RV mock_X_GetTokenInfo__invalid_slotid (CK_X_FUNCTION_LIST *self, - CK_SLOT_ID slot_id, - CK_TOKEN_INFO_PTR info); - -CK_RV mock_C_GetTokenInfo__not_initialized (CK_SLOT_ID slot_id, - CK_TOKEN_INFO_PTR info); - -CK_RV mock_C_GetMechanismList (CK_SLOT_ID slot_id, - CK_MECHANISM_TYPE_PTR mechanism_list, - CK_ULONG_PTR count); - -CK_RV mock_C_GetMechanismList__invalid_slotid (CK_SLOT_ID slot_id, - CK_MECHANISM_TYPE_PTR mechanism_list, - CK_ULONG_PTR count); - -CK_RV mock_X_GetMechanismList__invalid_slotid (CK_X_FUNCTION_LIST *self, - CK_SLOT_ID slot_id, - CK_MECHANISM_TYPE_PTR mechanism_list, - CK_ULONG_PTR count); - -CK_RV mock_C_GetMechanismInfo (CK_SLOT_ID slot_id, - CK_MECHANISM_TYPE type, - CK_MECHANISM_INFO_PTR info); - -CK_RV mock_C_GetMechanismInfo__invalid_slotid (CK_SLOT_ID slot_id, - CK_MECHANISM_TYPE type, - CK_MECHANISM_INFO_PTR info); - -CK_RV mock_X_GetMechanismInfo__invalid_slotid (CK_X_FUNCTION_LIST *self, - CK_SLOT_ID slot_id, - CK_MECHANISM_TYPE type, - CK_MECHANISM_INFO_PTR info); - -CK_RV mock_C_InitToken__specific_args (CK_SLOT_ID slot_id, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len, - CK_UTF8CHAR_PTR label); - -CK_RV mock_C_InitToken__invalid_slotid (CK_SLOT_ID slot_id, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len, - CK_UTF8CHAR_PTR label); - -CK_RV mock_X_InitToken__invalid_slotid (CK_X_FUNCTION_LIST *self, - CK_SLOT_ID slot_id, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len, - CK_UTF8CHAR_PTR label); - - -CK_RV mock_C_WaitForSlotEvent (CK_FLAGS flags, - CK_SLOT_ID_PTR slot, - CK_VOID_PTR reserved); - -CK_RV mock_C_WaitForSlotEvent__no_event (CK_FLAGS flags, - CK_SLOT_ID_PTR slot, - CK_VOID_PTR reserved); - -CK_RV mock_X_WaitForSlotEvent__no_event (CK_X_FUNCTION_LIST *self, - CK_FLAGS flags, - CK_SLOT_ID_PTR slot, - CK_VOID_PTR reserved); - -CK_RV mock_C_OpenSession__invalid_slotid (CK_SLOT_ID slot_id, - CK_FLAGS flags, - CK_VOID_PTR user_data, - CK_NOTIFY callback, - CK_SESSION_HANDLE_PTR session); - -CK_RV mock_X_OpenSession__invalid_slotid (CK_X_FUNCTION_LIST *self, - CK_SLOT_ID slot_id, - CK_FLAGS flags, - CK_VOID_PTR user_data, - CK_NOTIFY callback, - CK_SESSION_HANDLE_PTR session); - -CK_RV mock_C_OpenSession__fails (CK_SLOT_ID slot_id, - CK_FLAGS flags, - CK_VOID_PTR user_data, - CK_NOTIFY callback, - CK_SESSION_HANDLE_PTR session); - -CK_RV mock_C_OpenSession (CK_SLOT_ID slot_id, - CK_FLAGS flags, - CK_VOID_PTR user_data, - CK_NOTIFY callback, - CK_SESSION_HANDLE_PTR session); - -CK_RV mock_C_CloseSession (CK_SESSION_HANDLE session); - -CK_RV mock_C_CloseSession__invalid_handle (CK_SESSION_HANDLE session); - -CK_RV mock_X_CloseSession__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session); - -CK_RV mock_C_CloseAllSessions (CK_SLOT_ID slot_id); - -CK_RV mock_C_CloseAllSessions__invalid_slotid (CK_SLOT_ID slot_id); - -CK_RV mock_X_CloseAllSessions__invalid_slotid (CK_X_FUNCTION_LIST *self, - CK_SLOT_ID slot_id); - -CK_RV mock_C_GetFunctionStatus (CK_SESSION_HANDLE session); - -CK_RV mock_C_GetFunctionStatus__not_parallel (CK_SESSION_HANDLE session); - -CK_RV mock_C_CancelFunction (CK_SESSION_HANDLE session); - -CK_RV mock_C_CancelFunction__not_parallel (CK_SESSION_HANDLE session); - -CK_RV mock_C_GetSessionInfo (CK_SESSION_HANDLE session, - CK_SESSION_INFO_PTR info); - -CK_RV mock_C_GetSessionInfo__invalid_handle (CK_SESSION_HANDLE session, - CK_SESSION_INFO_PTR info); - -CK_RV mock_X_GetSessionInfo__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_SESSION_INFO_PTR info); - -CK_RV mock_C_InitPIN__specific_args (CK_SESSION_HANDLE session, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len); - -CK_RV mock_C_InitPIN__invalid_handle (CK_SESSION_HANDLE session, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len); - -CK_RV mock_X_InitPIN__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len); - -CK_RV mock_C_SetPIN__specific_args (CK_SESSION_HANDLE session, - CK_UTF8CHAR_PTR old_pin, - CK_ULONG old_pin_len, - CK_UTF8CHAR_PTR new_pin, - CK_ULONG new_pin_len); - -CK_RV mock_C_SetPIN__invalid_handle (CK_SESSION_HANDLE session, - CK_UTF8CHAR_PTR old_pin, - CK_ULONG old_pin_len, - CK_UTF8CHAR_PTR new_pin, - CK_ULONG new_pin_len); - -CK_RV mock_X_SetPIN__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_UTF8CHAR_PTR old_pin, - CK_ULONG old_pin_len, - CK_UTF8CHAR_PTR new_pin, - CK_ULONG new_pin_len); - -CK_RV mock_C_GetOperationState (CK_SESSION_HANDLE session, - CK_BYTE_PTR operation_state, - CK_ULONG_PTR operation_state_len); - -CK_RV mock_C_GetOperationState__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR operation_state, - CK_ULONG_PTR operation_state_len); - -CK_RV mock_X_GetOperationState__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR operation_state, - CK_ULONG_PTR operation_state_len); - -CK_RV mock_C_SetOperationState (CK_SESSION_HANDLE session, - CK_BYTE_PTR operation_state, - CK_ULONG operation_state_len, - CK_OBJECT_HANDLE encryption_key, - CK_OBJECT_HANDLE authentication_key); - -CK_RV mock_C_SetOperationState__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR operation_state, - CK_ULONG operation_state_len, - CK_OBJECT_HANDLE encryption_key, - CK_OBJECT_HANDLE authentication_key); - -CK_RV mock_X_SetOperationState__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR operation_state, - CK_ULONG operation_state_len, - CK_OBJECT_HANDLE encryption_key, - CK_OBJECT_HANDLE authentication_key); - -CK_RV mock_C_Login (CK_SESSION_HANDLE session, - CK_USER_TYPE user_type, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len); - -CK_RV mock_C_Login__invalid_handle (CK_SESSION_HANDLE session, - CK_USER_TYPE user_type, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len); - -CK_RV mock_X_Login__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_USER_TYPE user_type, - CK_UTF8CHAR_PTR pin, - CK_ULONG pin_len); - -CK_RV mock_C_Logout (CK_SESSION_HANDLE session); - -CK_RV mock_C_Logout__invalid_handle (CK_SESSION_HANDLE session); - -CK_RV mock_X_Logout__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session); - -CK_RV mock_C_CreateObject (CK_SESSION_HANDLE session, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR object); - -CK_RV mock_C_CreateObject__invalid_handle (CK_SESSION_HANDLE session, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR new_object); - -CK_RV mock_X_CreateObject__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR new_object); - -CK_RV mock_C_CopyObject (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR new_object); - -CK_RV mock_C_CopyObject__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR new_object); - -CK_RV mock_X_CopyObject__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR new_object); - -CK_RV mock_C_DestroyObject (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object); - -CK_RV mock_C_DestroyObject__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object); - -CK_RV mock_X_DestroyObject__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object); - -CK_RV mock_C_GetObjectSize (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ULONG_PTR size); - -CK_RV mock_C_GetObjectSize__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ULONG_PTR size); - -CK_RV mock_X_GetObjectSize__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ULONG_PTR size); - -CK_RV mock_C_GetAttributeValue (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count); - -CK_RV mock_C_GetAttributeValue__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count); - -CK_RV mock_X_GetAttributeValue__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count); - -CK_RV mock_C_GetAttributeValue__fail_first (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count); - -CK_RV mock_C_GetAttributeValue__fail_late (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count); - -CK_RV mock_C_SetAttributeValue (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count); - -CK_RV mock_C_SetAttributeValue__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count); - -CK_RV mock_X_SetAttributeValue__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE object, - CK_ATTRIBUTE_PTR template, - CK_ULONG count); - -CK_RV mock_C_FindObjectsInit (CK_SESSION_HANDLE session, - CK_ATTRIBUTE_PTR template, - CK_ULONG count); - -CK_RV mock_C_FindObjectsInit__invalid_handle (CK_SESSION_HANDLE session, - CK_ATTRIBUTE_PTR template, - CK_ULONG count); - -CK_RV mock_X_FindObjectsInit__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_ATTRIBUTE_PTR template, - CK_ULONG count); - -CK_RV mock_C_FindObjectsInit__fails (CK_SESSION_HANDLE session, - CK_ATTRIBUTE_PTR template, - CK_ULONG count); - -CK_RV mock_C_FindObjects (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE_PTR objects, - CK_ULONG max_object_count, - CK_ULONG_PTR object_count); - -CK_RV mock_C_FindObjects__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE_PTR objects, - CK_ULONG max_count, - CK_ULONG_PTR count); - -CK_RV mock_X_FindObjects__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE_PTR objects, - CK_ULONG max_count, - CK_ULONG_PTR count); - -CK_RV mock_C_FindObjects__fails (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE_PTR objects, - CK_ULONG max_count, - CK_ULONG_PTR count); - -CK_RV mock_C_FindObjectsFinal (CK_SESSION_HANDLE session); - -CK_RV mock_C_FindObjectsFinal__invalid_handle (CK_SESSION_HANDLE session); - -CK_RV mock_X_FindObjectsFinal__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session); - -CK_RV mock_C_EncryptInit (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_C_EncryptInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_X_EncryptInit__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_C_Encrypt (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR encrypted_data, - CK_ULONG_PTR encrypted_data_len); - -CK_RV mock_C_Encrypt__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR encrypted_data, - CK_ULONG_PTR encrypted_data_len); - -CK_RV mock_X_Encrypt__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR encrypted_data, - CK_ULONG_PTR encrypted_data_len); - -CK_RV mock_C_EncryptUpdate (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, - CK_BYTE_PTR encrypted_part, - CK_ULONG_PTR encrypted_part_len); - -CK_RV mock_C_EncryptUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, - CK_BYTE_PTR encrypted_part, - CK_ULONG_PTR encrypted_part_len); - -CK_RV mock_X_EncryptUpdate__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, - CK_BYTE_PTR encrypted_part, - CK_ULONG_PTR encrypted_part_len); - -CK_RV mock_C_EncryptFinal (CK_SESSION_HANDLE session, - CK_BYTE_PTR last_encrypted_part, - CK_ULONG_PTR last_encrypted_part_len); - -CK_RV mock_C_EncryptFinal__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR last_part, - CK_ULONG_PTR last_part_len); - -CK_RV mock_X_EncryptFinal__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR last_part, - CK_ULONG_PTR last_part_len); - -CK_RV mock_C_DecryptInit (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_C_DecryptInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_X_DecryptInit__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_C_Decrypt (CK_SESSION_HANDLE session, - CK_BYTE_PTR encrypted_data, - CK_ULONG encrypted_data_len, - CK_BYTE_PTR data, - CK_ULONG_PTR data_len); - -CK_RV mock_C_Decrypt__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_data, - CK_ULONG enc_data_len, - CK_BYTE_PTR data, - CK_ULONG_PTR data_len); - -CK_RV mock_X_Decrypt__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_data, - CK_ULONG enc_data_len, - CK_BYTE_PTR data, - CK_ULONG_PTR data_len); - -CK_RV mock_C_DecryptUpdate (CK_SESSION_HANDLE session, - CK_BYTE_PTR encrypted_part, - CK_ULONG encrypted_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len); - -CK_RV mock_C_DecryptUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_part, - CK_ULONG enc_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len); - -CK_RV mock_X_DecryptUpdate__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_part, - CK_ULONG enc_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len); - -CK_RV mock_C_DecryptFinal (CK_SESSION_HANDLE session, - CK_BYTE_PTR last_part, - CK_ULONG_PTR last_part_len); - -CK_RV mock_C_DecryptFinal__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR last_part, - CK_ULONG_PTR last_part_len); - -CK_RV mock_X_DecryptFinal__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR last_part, - CK_ULONG_PTR last_part_len); - -CK_RV mock_C_DigestInit (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism); - -CK_RV mock_C_DigestInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism); - -CK_RV mock_X_DigestInit__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism); - -CK_RV mock_C_Digest (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR digest, - CK_ULONG_PTR digest_len); - -CK_RV mock_C_Digest__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR digest, - CK_ULONG_PTR digest_len); - -CK_RV mock_X_Digest__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR digest, - CK_ULONG_PTR digest_len); - -CK_RV mock_C_DigestUpdate (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len); - -CK_RV mock_C_DigestUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len); - -CK_RV mock_X_DigestUpdate__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len); - -CK_RV mock_C_DigestKey (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE key); - -CK_RV mock_C_DigestKey__invalid_handle (CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE key); - -CK_RV mock_X_DigestKey__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_OBJECT_HANDLE key); - -CK_RV mock_C_DigestFinal (CK_SESSION_HANDLE session, - CK_BYTE_PTR digest, - CK_ULONG_PTR digest_len); - -CK_RV mock_C_DigestFinal__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR digest, - CK_ULONG_PTR digest_len); - -CK_RV mock_X_DigestFinal__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR digest, - CK_ULONG_PTR digest_len); - -CK_RV mock_C_SignInit (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_C_SignInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_X_SignInit__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_C_Sign (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len); - -CK_RV mock_C_Sign__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len); - -CK_RV mock_X_Sign__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len); - -CK_RV mock_C_SignUpdate (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len); - -CK_RV mock_C_SignUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len); - -CK_RV mock_X_SignUpdate__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len); - -CK_RV mock_C_SignFinal (CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len); - -CK_RV mock_C_SignFinal__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len); - -CK_RV mock_X_SignFinal__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len); - -CK_RV mock_C_SignRecoverInit (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_C_SignRecoverInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_X_SignRecoverInit__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_C_SignRecover (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len); - -CK_RV mock_C_SignRecover__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len); - -CK_RV mock_X_SignRecover__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG_PTR signature_len); - -CK_RV mock_C_VerifyInit (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_C_VerifyInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_X_VerifyInit__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_C_Verify (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG signature_len); - -CK_RV mock_C_Verify__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG signature_len); - -CK_RV mock_X_Verify__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR data, - CK_ULONG data_len, - CK_BYTE_PTR signature, - CK_ULONG signature_len); - -CK_RV mock_C_VerifyUpdate (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len); - -CK_RV mock_C_VerifyUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len); - -CK_RV mock_X_VerifyUpdate__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len); - -CK_RV mock_C_VerifyFinal (CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG signature_len); - -CK_RV mock_C_VerifyFinal__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG signature_len); - -CK_RV mock_X_VerifyFinal__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG signature_len); - -CK_RV mock_C_VerifyRecoverInit (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_C_VerifyRecoverInit__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_X_VerifyRecoverInit__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE key); - -CK_RV mock_C_VerifyRecover (CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG signature_len, - CK_BYTE_PTR data, - CK_ULONG_PTR data_len); - -CK_RV mock_C_VerifyRecover__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG signature_len, - CK_BYTE_PTR data, - CK_ULONG_PTR data_len); - -CK_RV mock_X_VerifyRecover__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR signature, - CK_ULONG signature_len, - CK_BYTE_PTR data, - CK_ULONG_PTR data_len); - -CK_RV mock_C_DigestEncryptUpdate (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, - CK_BYTE_PTR encrypted_part, - CK_ULONG_PTR encrypted_part_len); - -CK_RV mock_C_DigestEncryptUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, - CK_BYTE_PTR enc_part, - CK_ULONG_PTR enc_part_len); - -CK_RV mock_X_DigestEncryptUpdate__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, - CK_BYTE_PTR enc_part, - CK_ULONG_PTR enc_part_len); - -CK_RV mock_C_DecryptDigestUpdate (CK_SESSION_HANDLE session, - CK_BYTE_PTR encrypted_part, - CK_ULONG encrypted_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len); - -CK_RV mock_C_DecryptDigestUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_part, - CK_ULONG enc_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len); - -CK_RV mock_X_DecryptDigestUpdate__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_part, - CK_ULONG enc_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len); - -CK_RV mock_C_SignEncryptUpdate (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, - CK_BYTE_PTR encrypted_part, - CK_ULONG_PTR encrypted_part_len); - -CK_RV mock_C_SignEncryptUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, - CK_BYTE_PTR enc_part, - CK_ULONG_PTR enc_part_len); - -CK_RV mock_X_SignEncryptUpdate__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR part, - CK_ULONG part_len, - CK_BYTE_PTR enc_part, - CK_ULONG_PTR enc_part_len); - -CK_RV mock_C_DecryptVerifyUpdate (CK_SESSION_HANDLE session, - CK_BYTE_PTR encrypted_part, - CK_ULONG encrypted_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len); - -CK_RV mock_C_DecryptVerifyUpdate__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_part, - CK_ULONG enc_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len); - -CK_RV mock_X_DecryptVerifyUpdate__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR enc_part, - CK_ULONG enc_part_len, - CK_BYTE_PTR part, - CK_ULONG_PTR part_len); - -CK_RV mock_C_GenerateKey (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key); - -CK_RV mock_C_GenerateKey__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key); - -CK_RV mock_X_GenerateKey__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key); - -CK_RV mock_C_GenerateKeyPair (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_ATTRIBUTE_PTR public_key_template, - CK_ULONG public_key_count, - CK_ATTRIBUTE_PTR private_key_template, - CK_ULONG private_key_count, - CK_OBJECT_HANDLE_PTR public_key, - CK_OBJECT_HANDLE_PTR private_key); - -CK_RV mock_C_GenerateKeyPair__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_ATTRIBUTE_PTR pub_template, - CK_ULONG pub_count, - CK_ATTRIBUTE_PTR priv_template, - CK_ULONG priv_count, - CK_OBJECT_HANDLE_PTR pub_key, - CK_OBJECT_HANDLE_PTR priv_key); - -CK_RV mock_X_GenerateKeyPair__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_ATTRIBUTE_PTR pub_template, - CK_ULONG pub_count, - CK_ATTRIBUTE_PTR priv_template, - CK_ULONG priv_count, - CK_OBJECT_HANDLE_PTR pub_key, - CK_OBJECT_HANDLE_PTR priv_key); - -CK_RV mock_C_WrapKey (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE wrapping_key, - CK_OBJECT_HANDLE key, - CK_BYTE_PTR wrapped_key, - CK_ULONG_PTR wrapped_key_len); - -CK_RV mock_C_WrapKey__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE wrapping_key, - CK_OBJECT_HANDLE key, - CK_BYTE_PTR wrapped_key, - CK_ULONG_PTR wrapped_key_len); - -CK_RV mock_X_WrapKey__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE wrapping_key, - CK_OBJECT_HANDLE key, - CK_BYTE_PTR wrapped_key, - CK_ULONG_PTR wrapped_key_len); - -CK_RV mock_C_UnwrapKey (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE unwrapping_key, - CK_BYTE_PTR wrapped_key, - CK_ULONG wrapped_key_len, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key); - -CK_RV mock_C_UnwrapKey__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE unwrapping_key, - CK_BYTE_PTR wrapped_key, - CK_ULONG wrapped_key_len, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key); - -CK_RV mock_X_UnwrapKey__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE unwrapping_key, - CK_BYTE_PTR wrapped_key, - CK_ULONG wrapped_key_len, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key); - -CK_RV mock_C_DeriveKey (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE base_key, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key); - -CK_RV mock_C_DeriveKey__invalid_handle (CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE base_key, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key); - -CK_RV mock_X_DeriveKey__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_MECHANISM_PTR mechanism, - CK_OBJECT_HANDLE base_key, - CK_ATTRIBUTE_PTR template, - CK_ULONG count, - CK_OBJECT_HANDLE_PTR key); - -CK_RV mock_C_SeedRandom (CK_SESSION_HANDLE session, - CK_BYTE_PTR seed, - CK_ULONG seed_len); - -CK_RV mock_C_SeedRandom__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR seed, - CK_ULONG seed_len); - -CK_RV mock_X_SeedRandom__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR seed, - CK_ULONG seed_len); - -CK_RV mock_C_GenerateRandom (CK_SESSION_HANDLE session, - CK_BYTE_PTR random_data, - CK_ULONG random_len); - -CK_RV mock_C_GenerateRandom__invalid_handle (CK_SESSION_HANDLE session, - CK_BYTE_PTR random_data, - CK_ULONG random_len); - -CK_RV mock_X_GenerateRandom__invalid_handle (CK_X_FUNCTION_LIST *self, - CK_SESSION_HANDLE session, - CK_BYTE_PTR random_data, - CK_ULONG random_len); - -#endif /* __MOCK_H__ */ diff --git a/common/path.c b/common/path.c deleted file mode 100644 index 34c00cb..0000000 --- a/common/path.c +++ /dev/null @@ -1,325 +0,0 @@ -/* - * Copyright (c) 2005 Stefan Walter - * Copyright (c) 2011 Collabora Ltd. - * Copyright (c) 2013 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. - * - * - * CONTRIBUTORS - * Stef Walter <stefw@redhat.com> - */ - -#include "config.h" - -#include "debug.h" -#include "message.h" -#include "path.h" - -#include <assert.h> -#include <errno.h> -#include <stdarg.h> -#include <stdlib.h> -#include <string.h> - -#ifdef OS_UNIX -#include <pwd.h> -#include <unistd.h> -#endif - -#ifdef OS_WIN32 -#include <shlobj.h> -#endif - - -char * -p11_path_base (const char *path) -{ -#ifdef OS_WIN32 - const char *delims = "/\\"; -#else - const char *delims = "/"; -#endif - - const char *end; - const char *beg; - - return_val_if_fail (path != NULL, NULL); - - /* Any trailing slashes */ - end = path + strlen (path); - while (end != path) { - if (!strchr (delims, *(end - 1))) - break; - end--; - } - - /* Find the last slash after those */ - beg = end; - while (beg != path) { - if (strchr (delims, *(beg - 1))) - break; - beg--; - } - - return strndup (beg, end - beg); -} - -static inline bool -is_path_component_or_null (char ch) -{ - return (ch == '\0' || ch == '/' -#ifdef OS_WIN32 - || ch == '\\' -#endif - ); -} - -static char * -expand_homedir (const char *remainder) -{ - const char *env; - - if (getauxval (AT_SECURE)) { - errno = EPERM; - return NULL; - } - - while (remainder[0] && is_path_component_or_null (remainder[0])) - remainder++; - if (remainder[0] == '\0') - remainder = NULL; - - /* Expand $XDG_CONFIG_HOME */ - if (remainder != NULL && - strncmp (remainder, ".config", 7) == 0 && - is_path_component_or_null (remainder[7])) { - env = getenv ("XDG_CONFIG_HOME"); - if (env && env[0]) - return p11_path_build (env, remainder + 8, NULL); - } - - env = getenv ("HOME"); - if (env && env[0]) { - return p11_path_build (env, remainder, NULL); - - } else { -#ifdef OS_UNIX - char buf[1024]; - struct passwd pws; - struct passwd *pwd = NULL; - int error; - int ret; - - errno = 0; - ret = getpwuid_r (getuid (), &pws, buf, sizeof (buf), &pwd); - if (pwd == NULL) { - if (ret == 0) - error = ESRCH; - else - error = errno; - p11_message_err (error, "couldn't lookup home directory for user %d", getuid ()); - errno = error; - return NULL; - } - - return p11_path_build (pwd->pw_dir, remainder, NULL); - -#else /* OS_WIN32 */ - char directory[MAX_PATH + 1]; - - if (!SHGetSpecialFolderPathA (NULL, directory, CSIDL_PROFILE, TRUE)) { - p11_message ("couldn't lookup home directory for user"); - errno = ENOTDIR; - return NULL; - } - - return p11_path_build (directory, remainder, NULL); - -#endif /* OS_WIN32 */ - } -} - -char * -p11_path_expand (const char *path) -{ - return_val_if_fail (path != NULL, NULL); - - if (strncmp (path, "~", 1) == 0 && - is_path_component_or_null (path[1])) { - return expand_homedir (path + 1); - - } else { - return strdup (path); - } -} - -bool -p11_path_absolute (const char *path) -{ - return_val_if_fail (path != NULL, false); - - return (path[0] == '/') -#ifdef OS_WIN32 - || (path[0] != '\0' && path[1] == ':' && path[2] == '\\') -#endif - ; -} - -char * -p11_path_build (const char *path, - ...) -{ -#ifdef OS_WIN32 - const char delim = '\\'; -#else - const char delim = '/'; -#endif - const char *first = path; - char *built; - size_t len; - size_t at; - size_t num; - size_t until; - va_list va; - - return_val_if_fail (path != NULL, NULL); - - len = 1; - va_start (va, path); - while (path != NULL) { - len += strlen (path) + 1; - path = va_arg (va, const char *); - } - va_end (va); - - built = malloc (len + 1); - return_val_if_fail (built != NULL, NULL); - - at = 0; - path = first; - va_start (va, path); - while (path != NULL) { - num = strlen (path); - - /* Trim end of the path */ - until = (at > 0) ? 0 : 1; - while (num > until && is_path_component_or_null (path[num - 1])) - num--; - - if (at != 0) { - if (num == 0) - continue; - built[at++] = delim; - } - - assert (at + num < len); - memcpy (built + at, path, num); - at += num; - - path = va_arg (va, const char *); - - /* Trim beginning of path */ - while (path && path[0] && is_path_component_or_null (path[0])) - path++; - } - va_end (va); - - assert (at < len); - built[at] = '\0'; - return built; -} - -char * -p11_path_parent (const char *path) -{ - const char *e; - char *parent; - bool had = false; - - return_val_if_fail (path != NULL, NULL); - - /* Find the end of the last component */ - e = path + strlen (path); - while (e != path && is_path_component_or_null (*e)) - e--; - - /* Find the beginning of the last component */ - while (e != path && !is_path_component_or_null (*e)) { - had = true; - e--; - } - - /* Find the end of the last component */ - while (e != path && is_path_component_or_null (*e)) - e--; - - if (e == path) { - if (!had) - return NULL; - parent = strdup ("/"); - } else { - parent = strndup (path, (e - path) + 1); - } - - return_val_if_fail (parent != NULL, NULL); - return parent; -} - -bool -p11_path_prefix (const char *string, - const char *prefix) -{ - int a, b; - - return_val_if_fail (string != NULL, false); - return_val_if_fail (prefix != NULL, false); - - a = strlen (string); - b = strlen (prefix); - - return a > b && - strncmp (string, prefix, b) == 0 && - is_path_component_or_null (string[b]); -} - -void -p11_path_canon (char *name) -{ - static const char *VALID = - "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_"; - int i; - - return_if_fail (name != NULL); - - for (i = 0; name[i] != '\0'; i++) { - if (strchr (VALID, name[i]) == NULL) - name[i] = '_'; - } -} diff --git a/common/path.h b/common/path.h deleted file mode 100644 index 0b19a5d..0000000 --- a/common/path.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (c) 2013 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@redhat.com> - */ - -#ifndef P11_PATH_H__ -#define P11_PATH_H__ - -#include "compat.h" - -#ifdef OS_WIN32 -#define P11_PATH_SEP ";" -#define P11_PATH_SEP_C ';' -#else -#define P11_PATH_SEP ":" -#define P11_PATH_SEP_C ':' -#endif - -/* - * The semantics of both POSIX basename() and GNU asename() are so crappy that - * we just don't even bother. And what's worse is how it completely changes - * behavior if _GNU_SOURCE is defined. Nasty stuff. - */ -char * p11_path_base (const char *name); - -char * p11_path_expand (const char *path); - -char * p11_path_build (const char *path, - ...) GNUC_NULL_TERMINATED; - -bool p11_path_absolute (const char *path); - -char * p11_path_parent (const char *path); - -bool p11_path_prefix (const char *string, - const char *prefix); - -void p11_path_canon (char *name); - -#endif /* P11_PATH_H__ */ diff --git a/common/pkcs11.h b/common/pkcs11.h deleted file mode 100644 index f8dc78e..0000000 --- a/common/pkcs11.h +++ /dev/null @@ -1,1398 +0,0 @@ -/* pkcs11.h - Copyright 2006, 2007 g10 Code GmbH - Copyright 2006 Andreas Jellinghaus - - This file is free software; as a special exception the author gives - unlimited permission to copy and/or distribute it, with or without - modifications, as long as this notice is preserved. - - This file is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY, to the extent permitted by law; without even - the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR - PURPOSE. */ - -/* Please submit changes back to the Scute project at - http://www.scute.org/ (or send them to marcus@g10code.com), so that - they can be picked up by other projects from there as well. */ - -/* This file is a modified implementation of the PKCS #11 standard by - RSA Security Inc. It is mostly a drop-in replacement, with the - following change: - - This header file does not require any macro definitions by the user - (like CK_DEFINE_FUNCTION etc). In fact, it defines those macros - for you (if useful, some are missing, let me know if you need - more). - - There is an additional API available that does comply better to the - GNU coding standard. It can be switched on by defining - CRYPTOKI_GNU before including this header file. For this, the - following changes are made to the specification: - - All structure types are changed to a "struct ck_foo" where CK_FOO - is the type name in PKCS #11. - - All non-structure types are changed to ck_foo_t where CK_FOO is the - lowercase version of the type name in PKCS #11. The basic types - (CK_ULONG et al.) are removed without substitute. - - All members of structures are modified in the following way: Type - indication prefixes are removed, and underscore characters are - inserted before words. Then the result is lowercased. - - Note that function names are still in the original case, as they - need for ABI compatibility. - - CK_FALSE, CK_TRUE and NULL_PTR are removed without substitute. Use - <stdbool.h>. - - If CRYPTOKI_COMPAT is defined before including this header file, - then none of the API changes above take place, and the API is the - one defined by the PKCS #11 standard. */ - -#ifndef PKCS11_H -#define PKCS11_H 1 - -#if defined(__cplusplus) -extern "C" { -#endif - - -/* The version of cryptoki we implement. The revision is changed with - each modification of this file. If you do not use the "official" - version of this file, please consider deleting the revision macro - (you may use a macro with a different name to keep track of your - versions). */ -#define CRYPTOKI_VERSION_MAJOR 2 -#define CRYPTOKI_VERSION_MINOR 20 -#define CRYPTOKI_VERSION_REVISION 6 - - -/* Compatibility interface is default, unless CRYPTOKI_GNU is - given. */ -#ifndef CRYPTOKI_GNU -#ifndef CRYPTOKI_COMPAT -#define CRYPTOKI_COMPAT 1 -#endif -#endif - -/* System dependencies. */ - -#if defined(_WIN32) || defined(CRYPTOKI_FORCE_WIN32) - -/* There is a matching pop below. */ -#pragma pack(push, cryptoki, 1) - -#ifdef CRYPTOKI_EXPORTS -#define CK_SPEC __declspec(dllexport) -#else -#define CK_SPEC __declspec(dllimport) -#endif - -#else - -#define CK_SPEC - -#endif - - -#ifdef CRYPTOKI_COMPAT - /* If we are in compatibility mode, switch all exposed names to the - PKCS #11 variant. There are corresponding #undefs below. */ - -#define ck_flags_t CK_FLAGS -#define ck_version _CK_VERSION - -#define ck_info _CK_INFO -#define cryptoki_version cryptokiVersion -#define manufacturer_id manufacturerID -#define library_description libraryDescription -#define library_version libraryVersion - -#define ck_notification_t CK_NOTIFICATION -#define ck_slot_id_t CK_SLOT_ID - -#define ck_slot_info _CK_SLOT_INFO -#define slot_description slotDescription -#define hardware_version hardwareVersion -#define firmware_version firmwareVersion - -#define ck_token_info _CK_TOKEN_INFO -#define serial_number serialNumber -#define max_session_count ulMaxSessionCount -#define session_count ulSessionCount -#define max_rw_session_count ulMaxRwSessionCount -#define rw_session_count ulRwSessionCount -#define max_pin_len ulMaxPinLen -#define min_pin_len ulMinPinLen -#define total_public_memory ulTotalPublicMemory -#define free_public_memory ulFreePublicMemory -#define total_private_memory ulTotalPrivateMemory -#define free_private_memory ulFreePrivateMemory -#define utc_time utcTime - -#define ck_session_handle_t CK_SESSION_HANDLE -#define ck_user_type_t CK_USER_TYPE -#define ck_state_t CK_STATE - -#define ck_session_info _CK_SESSION_INFO -#define slot_id slotID -#define device_error ulDeviceError - -#define ck_object_handle_t CK_OBJECT_HANDLE -#define ck_object_class_t CK_OBJECT_CLASS -#define ck_hw_feature_type_t CK_HW_FEATURE_TYPE -#define ck_key_type_t CK_KEY_TYPE -#define ck_certificate_type_t CK_CERTIFICATE_TYPE -#define ck_attribute_type_t CK_ATTRIBUTE_TYPE - -#define ck_attribute _CK_ATTRIBUTE -#define value pValue -#define value_len ulValueLen - -#define ck_date _CK_DATE - -#define ck_mechanism_type_t CK_MECHANISM_TYPE - -#define ck_mechanism _CK_MECHANISM -#define parameter pParameter -#define parameter_len ulParameterLen - -#define ck_mechanism_info _CK_MECHANISM_INFO -#define min_key_size ulMinKeySize -#define max_key_size ulMaxKeySize - -#define ck_rv_t CK_RV -#define ck_notify_t CK_NOTIFY - -#define ck_function_list _CK_FUNCTION_LIST - -#define ck_createmutex_t CK_CREATEMUTEX -#define ck_destroymutex_t CK_DESTROYMUTEX -#define ck_lockmutex_t CK_LOCKMUTEX -#define ck_unlockmutex_t CK_UNLOCKMUTEX - -#define ck_c_initialize_args _CK_C_INITIALIZE_ARGS -#define create_mutex CreateMutex -#define destroy_mutex DestroyMutex -#define lock_mutex LockMutex -#define unlock_mutex UnlockMutex -#define reserved pReserved - -#endif /* CRYPTOKI_COMPAT */ - - - -typedef unsigned long ck_flags_t; - -struct ck_version -{ - unsigned char major; - unsigned char minor; -}; - - -struct ck_info -{ - struct ck_version cryptoki_version; - unsigned char manufacturer_id[32]; - ck_flags_t flags; - unsigned char library_description[32]; - struct ck_version library_version; -}; - - -typedef unsigned long ck_notification_t; - -#define CKN_SURRENDER (0UL) - - -typedef unsigned long ck_slot_id_t; - - -struct ck_slot_info -{ - unsigned char slot_description[64]; - unsigned char manufacturer_id[32]; - ck_flags_t flags; - struct ck_version hardware_version; - struct ck_version firmware_version; -}; - - -#define CKF_TOKEN_PRESENT (1UL << 0) -#define CKF_REMOVABLE_DEVICE (1UL << 1) -#define CKF_HW_SLOT (1UL << 2) -#define CKF_ARRAY_ATTRIBUTE (1UL << 30) - - -struct ck_token_info -{ - unsigned char label[32]; - unsigned char manufacturer_id[32]; - unsigned char model[16]; - unsigned char serial_number[16]; - ck_flags_t flags; - unsigned long max_session_count; - unsigned long session_count; - unsigned long max_rw_session_count; - unsigned long rw_session_count; - unsigned long max_pin_len; - unsigned long min_pin_len; - unsigned long total_public_memory; - unsigned long free_public_memory; - unsigned long total_private_memory; - unsigned long free_private_memory; - struct ck_version hardware_version; - struct ck_version firmware_version; - unsigned char utc_time[16]; -}; - - -#define CKF_RNG (1UL << 0) -#define CKF_WRITE_PROTECTED (1UL << 1) -#define CKF_LOGIN_REQUIRED (1UL << 2) -#define CKF_USER_PIN_INITIALIZED (1UL << 3) -#define CKF_RESTORE_KEY_NOT_NEEDED (1UL << 5) -#define CKF_CLOCK_ON_TOKEN (1UL << 6) -#define CKF_PROTECTED_AUTHENTICATION_PATH (1UL << 8) -#define CKF_DUAL_CRYPTO_OPERATIONS (1UL << 9) -#define CKF_TOKEN_INITIALIZED (1UL << 10) -#define CKF_SECONDARY_AUTHENTICATION (1UL << 11) -#define CKF_USER_PIN_COUNT_LOW (1UL << 16) -#define CKF_USER_PIN_FINAL_TRY (1UL << 17) -#define CKF_USER_PIN_LOCKED (1UL << 18) -#define CKF_USER_PIN_TO_BE_CHANGED (1UL << 19) -#define CKF_SO_PIN_COUNT_LOW (1UL << 20) -#define CKF_SO_PIN_FINAL_TRY (1UL << 21) -#define CKF_SO_PIN_LOCKED (1UL << 22) -#define CKF_SO_PIN_TO_BE_CHANGED (1UL << 23) - -#define CK_UNAVAILABLE_INFORMATION ((unsigned long)-1L) -#define CK_EFFECTIVELY_INFINITE (0UL) - - -typedef unsigned long ck_session_handle_t; - -#define CK_INVALID_HANDLE (0UL) - - -typedef unsigned long ck_user_type_t; - -#define CKU_SO (0UL) -#define CKU_USER (1UL) -#define CKU_CONTEXT_SPECIFIC (2UL) - - -typedef unsigned long ck_state_t; - -#define CKS_RO_PUBLIC_SESSION (0UL) -#define CKS_RO_USER_FUNCTIONS (1UL) -#define CKS_RW_PUBLIC_SESSION (2UL) -#define CKS_RW_USER_FUNCTIONS (3UL) -#define CKS_RW_SO_FUNCTIONS (4UL) - - -struct ck_session_info -{ - ck_slot_id_t slot_id; - ck_state_t state; - ck_flags_t flags; - unsigned long device_error; -}; - -#define CKF_RW_SESSION (1UL << 1) -#define CKF_SERIAL_SESSION (1UL << 2) - - -typedef unsigned long ck_object_handle_t; - - -typedef unsigned long ck_object_class_t; - -#define CKO_DATA (0UL) -#define CKO_CERTIFICATE (1UL) -#define CKO_PUBLIC_KEY (2UL) -#define CKO_PRIVATE_KEY (3UL) -#define CKO_SECRET_KEY (4UL) -#define CKO_HW_FEATURE (5UL) -#define CKO_DOMAIN_PARAMETERS (6UL) -#define CKO_MECHANISM (7UL) -#define CKO_VENDOR_DEFINED ((unsigned long) (1UL << 31)) - - -typedef unsigned long ck_hw_feature_type_t; - -#define CKH_MONOTONIC_COUNTER (1UL) -#define CKH_CLOCK (2UL) -#define CKH_USER_INTERFACE (3UL) -#define CKH_VENDOR_DEFINED ((unsigned long) (1UL << 31)) - - -typedef unsigned long ck_key_type_t; - -#define CKK_RSA (0UL) -#define CKK_DSA (1UL) -#define CKK_DH (2UL) -#define CKK_ECDSA (3UL) -#define CKK_EC (3UL) -#define CKK_X9_42_DH (4UL) -#define CKK_KEA (5UL) -#define CKK_GENERIC_SECRET (0x10UL) -#define CKK_RC2 (0x11UL) -#define CKK_RC4 (0x12UL) -#define CKK_DES (0x13UL) -#define CKK_DES2 (0x14UL) -#define CKK_DES3 (0x15UL) -#define CKK_CAST (0x16UL) -#define CKK_CAST3 (0x17UL) -#define CKK_CAST128 (0x18UL) -#define CKK_RC5 (0x19UL) -#define CKK_IDEA (0x1aUL) -#define CKK_SKIPJACK (0x1bUL) -#define CKK_BATON (0x1cUL) -#define CKK_JUNIPER (0x1dUL) -#define CKK_CDMF (0x1eUL) -#define CKK_AES (0x1fUL) -#define CKK_BLOWFISH (0x20UL) -#define CKK_TWOFISH (0x21UL) -#define CKK_VENDOR_DEFINED ((unsigned long) (1UL << 31)) - - -typedef unsigned long ck_certificate_type_t; - -#define CKC_X_509 (0UL) -#define CKC_X_509_ATTR_CERT (1UL) -#define CKC_WTLS (2UL) -#define CKC_VENDOR_DEFINED ((unsigned long) (1UL << 31)) - - -typedef unsigned long ck_attribute_type_t; - -#define CKA_CLASS (0UL) -#define CKA_TOKEN (1UL) -#define CKA_PRIVATE (2UL) -#define CKA_LABEL (3UL) -#define CKA_APPLICATION (0x10UL) -#define CKA_VALUE (0x11UL) -#define CKA_OBJECT_ID (0x12UL) -#define CKA_CERTIFICATE_TYPE (0x80UL) -#define CKA_ISSUER (0x81UL) -#define CKA_SERIAL_NUMBER (0x82UL) -#define CKA_AC_ISSUER (0x83UL) -#define CKA_OWNER (0x84UL) -#define CKA_ATTR_TYPES (0x85UL) -#define CKA_TRUSTED (0x86UL) -#define CKA_CERTIFICATE_CATEGORY (0x87UL) -#define CKA_JAVA_MIDP_SECURITY_DOMAIN (0x88UL) -#define CKA_URL (0x89UL) -#define CKA_HASH_OF_SUBJECT_PUBLIC_KEY (0x8aUL) -#define CKA_HASH_OF_ISSUER_PUBLIC_KEY (0x8bUL) -#define CKA_CHECK_VALUE (0x90UL) -#define CKA_KEY_TYPE (0x100UL) -#define CKA_SUBJECT (0x101UL) -#define CKA_ID (0x102UL) -#define CKA_SENSITIVE (0x103UL) -#define CKA_ENCRYPT (0x104UL) -#define CKA_DECRYPT (0x105UL) -#define CKA_WRAP (0x106UL) -#define CKA_UNWRAP (0x107UL) -#define CKA_SIGN (0x108UL) -#define CKA_SIGN_RECOVER (0x109UL) -#define CKA_VERIFY (0x10aUL) -#define CKA_VERIFY_RECOVER (0x10bUL) -#define CKA_DERIVE (0x10cUL) -#define CKA_START_DATE (0x110UL) -#define CKA_END_DATE (0x111UL) -#define CKA_MODULUS (0x120UL) -#define CKA_MODULUS_BITS (0x121UL) -#define CKA_PUBLIC_EXPONENT (0x122UL) -#define CKA_PRIVATE_EXPONENT (0x123UL) -#define CKA_PRIME_1 (0x124UL) -#define CKA_PRIME_2 (0x125UL) -#define CKA_EXPONENT_1 (0x126UL) -#define CKA_EXPONENT_2 (0x127UL) -#define CKA_COEFFICIENT (0x128UL) -#define CKA_PRIME (0x130UL) -#define CKA_SUBPRIME (0x131UL) -#define CKA_BASE (0x132UL) -#define CKA_PRIME_BITS (0x133UL) -#define CKA_SUB_PRIME_BITS (0x134UL) -#define CKA_VALUE_BITS (0x160UL) -#define CKA_VALUE_LEN (0x161UL) -#define CKA_EXTRACTABLE (0x162UL) -#define CKA_LOCAL (0x163UL) -#define CKA_NEVER_EXTRACTABLE (0x164UL) -#define CKA_ALWAYS_SENSITIVE (0x165UL) -#define CKA_KEY_GEN_MECHANISM (0x166UL) -#define CKA_MODIFIABLE (0x170UL) -#define CKA_ECDSA_PARAMS (0x180UL) -#define CKA_EC_PARAMS (0x180UL) -#define CKA_EC_POINT (0x181UL) -#define CKA_SECONDARY_AUTH (0x200UL) -#define CKA_AUTH_PIN_FLAGS (0x201UL) -#define CKA_ALWAYS_AUTHENTICATE (0x202UL) -#define CKA_WRAP_WITH_TRUSTED (0x210UL) -#define CKA_HW_FEATURE_TYPE (0x300UL) -#define CKA_RESET_ON_INIT (0x301UL) -#define CKA_HAS_RESET (0x302UL) -#define CKA_PIXEL_X (0x400UL) -#define CKA_PIXEL_Y (0x401UL) -#define CKA_RESOLUTION (0x402UL) -#define CKA_CHAR_ROWS (0x403UL) -#define CKA_CHAR_COLUMNS (0x404UL) -#define CKA_COLOR (0x405UL) -#define CKA_BITS_PER_PIXEL (0x406UL) -#define CKA_CHAR_SETS (0x480UL) -#define CKA_ENCODING_METHODS (0x481UL) -#define CKA_MIME_TYPES (0x482UL) -#define CKA_MECHANISM_TYPE (0x500UL) -#define CKA_REQUIRED_CMS_ATTRIBUTES (0x501UL) -#define CKA_DEFAULT_CMS_ATTRIBUTES (0x502UL) -#define CKA_SUPPORTED_CMS_ATTRIBUTES (0x503UL) -#define CKA_WRAP_TEMPLATE (CKF_ARRAY_ATTRIBUTE | 0x211UL) -#define CKA_UNWRAP_TEMPLATE (CKF_ARRAY_ATTRIBUTE | 0x212UL) -#define CKA_ALLOWED_MECHANISMS (CKF_ARRAY_ATTRIBUTE | 0x600UL) -#define CKA_VENDOR_DEFINED ((unsigned long) (1UL << 31)) - - -struct ck_attribute -{ - ck_attribute_type_t type; - void *value; - unsigned long value_len; -}; - - -struct ck_date -{ - unsigned char year[4]; - unsigned char month[2]; - unsigned char day[2]; -}; - - -typedef unsigned long ck_mechanism_type_t; - -#define CKM_RSA_PKCS_KEY_PAIR_GEN (0UL) -#define CKM_RSA_PKCS (1UL) -#define CKM_RSA_9796 (2UL) -#define CKM_RSA_X_509 (3UL) -#define CKM_MD2_RSA_PKCS (4UL) -#define CKM_MD5_RSA_PKCS (5UL) -#define CKM_SHA1_RSA_PKCS (6UL) -#define CKM_RIPEMD128_RSA_PKCS (7UL) -#define CKM_RIPEMD160_RSA_PKCS (8UL) -#define CKM_RSA_PKCS_OAEP (9UL) -#define CKM_RSA_X9_31_KEY_PAIR_GEN (0xaUL) -#define CKM_RSA_X9_31 (0xbUL) -#define CKM_SHA1_RSA_X9_31 (0xcUL) -#define CKM_RSA_PKCS_PSS (0xdUL) -#define CKM_SHA1_RSA_PKCS_PSS (0xeUL) -#define CKM_DSA_KEY_PAIR_GEN (0x10UL) -#define CKM_DSA (0x11UL) -#define CKM_DSA_SHA1 (0x12UL) -#define CKM_DH_PKCS_KEY_PAIR_GEN (0x20UL) -#define CKM_DH_PKCS_DERIVE (0x21UL) -#define CKM_X9_42_DH_KEY_PAIR_GEN (0x30UL) -#define CKM_X9_42_DH_DERIVE (0x31UL) -#define CKM_X9_42_DH_HYBRID_DERIVE (0x32UL) -#define CKM_X9_42_MQV_DERIVE (0x33UL) -#define CKM_SHA256_RSA_PKCS (0x40UL) -#define CKM_SHA384_RSA_PKCS (0x41UL) -#define CKM_SHA512_RSA_PKCS (0x42UL) -#define CKM_SHA256_RSA_PKCS_PSS (0x43UL) -#define CKM_SHA384_RSA_PKCS_PSS (0x44UL) -#define CKM_SHA512_RSA_PKCS_PSS (0x45UL) -#define CKM_RC2_KEY_GEN (0x100UL) -#define CKM_RC2_ECB (0x101UL) -#define CKM_RC2_CBC (0x102UL) -#define CKM_RC2_MAC (0x103UL) -#define CKM_RC2_MAC_GENERAL (0x104UL) -#define CKM_RC2_CBC_PAD (0x105UL) -#define CKM_RC4_KEY_GEN (0x110UL) -#define CKM_RC4 (0x111UL) -#define CKM_DES_KEY_GEN (0x120UL) -#define CKM_DES_ECB (0x121UL) -#define CKM_DES_CBC (0x122UL) -#define CKM_DES_MAC (0x123UL) -#define CKM_DES_MAC_GENERAL (0x124UL) -#define CKM_DES_CBC_PAD (0x125UL) -#define CKM_DES2_KEY_GEN (0x130UL) -#define CKM_DES3_KEY_GEN (0x131UL) -#define CKM_DES3_ECB (0x132UL) -#define CKM_DES3_CBC (0x133UL) -#define CKM_DES3_MAC (0x134UL) -#define CKM_DES3_MAC_GENERAL (0x135UL) -#define CKM_DES3_CBC_PAD (0x136UL) -#define CKM_CDMF_KEY_GEN (0x140UL) -#define CKM_CDMF_ECB (0x141UL) -#define CKM_CDMF_CBC (0x142UL) -#define CKM_CDMF_MAC (0x143UL) -#define CKM_CDMF_MAC_GENERAL (0x144UL) -#define CKM_CDMF_CBC_PAD (0x145UL) -#define CKM_DES_OFB64 (0x150UL) -#define CKM_DES_OFB8 (0x151UL) -#define CKM_DES_CFB64 (0x152UL) -#define CKM_DES_CFB8 (0x153UL) -#define CKM_MD2 (0x200UL) -#define CKM_MD2_HMAC (0x201UL) -#define CKM_MD2_HMAC_GENERAL (0x202UL) -#define CKM_MD5 (0x210UL) -#define CKM_MD5_HMAC (0x211UL) -#define CKM_MD5_HMAC_GENERAL (0x212UL) -#define CKM_SHA_1 (0x220UL) -#define CKM_SHA_1_HMAC (0x221UL) -#define CKM_SHA_1_HMAC_GENERAL (0x222UL) -#define CKM_RIPEMD128 (0x230UL) -#define CKM_RIPEMD128_HMAC (0x231UL) -#define CKM_RIPEMD128_HMAC_GENERAL (0x232UL) -#define CKM_RIPEMD160 (0x240UL) -#define CKM_RIPEMD160_HMAC (0x241UL) -#define CKM_RIPEMD160_HMAC_GENERAL (0x242UL) -#define CKM_SHA256 (0x250UL) -#define CKM_SHA256_HMAC (0x251UL) -#define CKM_SHA256_HMAC_GENERAL (0x252UL) -#define CKM_SHA384 (0x260UL) -#define CKM_SHA384_HMAC (0x261UL) -#define CKM_SHA384_HMAC_GENERAL (0x262UL) -#define CKM_SHA512 (0x270UL) -#define CKM_SHA512_HMAC (0x271UL) -#define CKM_SHA512_HMAC_GENERAL (0x272UL) -#define CKM_CAST_KEY_GEN (0x300UL) -#define CKM_CAST_ECB (0x301UL) -#define CKM_CAST_CBC (0x302UL) -#define CKM_CAST_MAC (0x303UL) -#define CKM_CAST_MAC_GENERAL (0x304UL) -#define CKM_CAST_CBC_PAD (0x305UL) -#define CKM_CAST3_KEY_GEN (0x310UL) -#define CKM_CAST3_ECB (0x311UL) -#define CKM_CAST3_CBC (0x312UL) -#define CKM_CAST3_MAC (0x313UL) -#define CKM_CAST3_MAC_GENERAL (0x314UL) -#define CKM_CAST3_CBC_PAD (0x315UL) -#define CKM_CAST5_KEY_GEN (0x320UL) -#define CKM_CAST128_KEY_GEN (0x320UL) -#define CKM_CAST5_ECB (0x321UL) -#define CKM_CAST128_ECB (0x321UL) -#define CKM_CAST5_CBC (0x322UL) -#define CKM_CAST128_CBC (0x322UL) -#define CKM_CAST5_MAC (0x323UL) -#define CKM_CAST128_MAC (0x323UL) -#define CKM_CAST5_MAC_GENERAL (0x324UL) -#define CKM_CAST128_MAC_GENERAL (0x324UL) -#define CKM_CAST5_CBC_PAD (0x325UL) -#define CKM_CAST128_CBC_PAD (0x325UL) -#define CKM_RC5_KEY_GEN (0x330UL) -#define CKM_RC5_ECB (0x331UL) -#define CKM_RC5_CBC (0x332UL) -#define CKM_RC5_MAC (0x333UL) -#define CKM_RC5_MAC_GENERAL (0x334UL) -#define CKM_RC5_CBC_PAD (0x335UL) -#define CKM_IDEA_KEY_GEN (0x340UL) -#define CKM_IDEA_ECB (0x341UL) -#define CKM_IDEA_CBC (0x342UL) -#define CKM_IDEA_MAC (0x343UL) -#define CKM_IDEA_MAC_GENERAL (0x344UL) -#define CKM_IDEA_CBC_PAD (0x345UL) -#define CKM_GENERIC_SECRET_KEY_GEN (0x350UL) -#define CKM_CONCATENATE_BASE_AND_KEY (0x360UL) -#define CKM_CONCATENATE_BASE_AND_DATA (0x362UL) -#define CKM_CONCATENATE_DATA_AND_BASE (0x363UL) -#define CKM_XOR_BASE_AND_DATA (0x364UL) -#define CKM_EXTRACT_KEY_FROM_KEY (0x365UL) -#define CKM_SSL3_PRE_MASTER_KEY_GEN (0x370UL) -#define CKM_SSL3_MASTER_KEY_DERIVE (0x371UL) -#define CKM_SSL3_KEY_AND_MAC_DERIVE (0x372UL) -#define CKM_SSL3_MASTER_KEY_DERIVE_DH (0x373UL) -#define CKM_TLS_PRE_MASTER_KEY_GEN (0x374UL) -#define CKM_TLS_MASTER_KEY_DERIVE (0x375UL) -#define CKM_TLS_KEY_AND_MAC_DERIVE (0x376UL) -#define CKM_TLS_MASTER_KEY_DERIVE_DH (0x377UL) -#define CKM_TLS_PRF (0x378UL) -#define CKM_SSL3_MD5_MAC (0x380UL) -#define CKM_SSL3_SHA1_MAC (0x381UL) -#define CKM_MD5_KEY_DERIVATION (0x390UL) -#define CKM_MD2_KEY_DERIVATION (0x391UL) -#define CKM_SHA1_KEY_DERIVATION (0x392UL) -#define CKM_SHA256_KEY_DERIVATION (0x393UL) -#define CKM_SHA384_KEY_DERIVATION (0x394UL) -#define CKM_SHA512_KEY_DERIVATION (0x395UL) -#define CKM_PBE_MD2_DES_CBC (0x3a0UL) -#define CKM_PBE_MD5_DES_CBC (0x3a1UL) -#define CKM_PBE_MD5_CAST_CBC (0x3a2UL) -#define CKM_PBE_MD5_CAST3_CBC (0x3a3UL) -#define CKM_PBE_MD5_CAST5_CBC (0x3a4UL) -#define CKM_PBE_MD5_CAST128_CBC (0x3a4UL) -#define CKM_PBE_SHA1_CAST5_CBC (0x3a5UL) -#define CKM_PBE_SHA1_CAST128_CBC (0x3a5UL) -#define CKM_PBE_SHA1_RC4_128 (0x3a6UL) -#define CKM_PBE_SHA1_RC4_40 (0x3a7UL) -#define CKM_PBE_SHA1_DES3_EDE_CBC (0x3a8UL) -#define CKM_PBE_SHA1_DES2_EDE_CBC (0x3a9UL) -#define CKM_PBE_SHA1_RC2_128_CBC (0x3aaUL) -#define CKM_PBE_SHA1_RC2_40_CBC (0x3abUL) -#define CKM_PKCS5_PBKD2 (0x3b0UL) -#define CKM_PBA_SHA1_WITH_SHA1_HMAC (0x3c0UL) -#define CKM_WTLS_PRE_MASTER_KEY_GEN (0x3d0UL) -#define CKM_WTLS_MASTER_KEY_DERIVE (0x3d1UL) -#define CKM_WTLS_MASTER_KEY_DERIVE_DH_ECC (0x3d2UL) -#define CKM_WTLS_PRF (0x3d3UL) -#define CKM_WTLS_SERVER_KEY_AND_MAC_DERIVE (0x3d4UL) -#define CKM_WTLS_CLIENT_KEY_AND_MAC_DERIVE (0x3d5UL) -#define CKM_KEY_WRAP_LYNKS (0x400UL) -#define CKM_KEY_WRAP_SET_OAEP (0x401UL) -#define CKM_CMS_SIG (0x500UL) -#define CKM_SKIPJACK_KEY_GEN (0x1000UL) -#define CKM_SKIPJACK_ECB64 (0x1001UL) -#define CKM_SKIPJACK_CBC64 (0x1002UL) -#define CKM_SKIPJACK_OFB64 (0x1003UL) -#define CKM_SKIPJACK_CFB64 (0x1004UL) -#define CKM_SKIPJACK_CFB32 (0x1005UL) -#define CKM_SKIPJACK_CFB16 (0x1006UL) -#define CKM_SKIPJACK_CFB8 (0x1007UL) -#define CKM_SKIPJACK_WRAP (0x1008UL) -#define CKM_SKIPJACK_PRIVATE_WRAP (0x1009UL) -#define CKM_SKIPJACK_RELAYX (0x100aUL) -#define CKM_KEA_KEY_PAIR_GEN (0x1010UL) -#define CKM_KEA_KEY_DERIVE (0x1011UL) -#define CKM_FORTEZZA_TIMESTAMP (0x1020UL) -#define CKM_BATON_KEY_GEN (0x1030UL) -#define CKM_BATON_ECB128 (0x1031UL) -#define CKM_BATON_ECB96 (0x1032UL) -#define CKM_BATON_CBC128 (0x1033UL) -#define CKM_BATON_COUNTER (0x1034UL) -#define CKM_BATON_SHUFFLE (0x1035UL) -#define CKM_BATON_WRAP (0x1036UL) -#define CKM_ECDSA_KEY_PAIR_GEN (0x1040UL) -#define CKM_EC_KEY_PAIR_GEN (0x1040UL) -#define CKM_ECDSA (0x1041UL) -#define CKM_ECDSA_SHA1 (0x1042UL) -#define CKM_ECDH1_DERIVE (0x1050UL) -#define CKM_ECDH1_COFACTOR_DERIVE (0x1051UL) -#define CKM_ECMQV_DERIVE (0x1052UL) -#define CKM_JUNIPER_KEY_GEN (0x1060UL) -#define CKM_JUNIPER_ECB128 (0x1061UL) -#define CKM_JUNIPER_CBC128 (0x1062UL) -#define CKM_JUNIPER_COUNTER (0x1063UL) -#define CKM_JUNIPER_SHUFFLE (0x1064UL) -#define CKM_JUNIPER_WRAP (0x1065UL) -#define CKM_FASTHASH (0x1070UL) -#define CKM_AES_KEY_GEN (0x1080UL) -#define CKM_AES_ECB (0x1081UL) -#define CKM_AES_CBC (0x1082UL) -#define CKM_AES_MAC (0x1083UL) -#define CKM_AES_MAC_GENERAL (0x1084UL) -#define CKM_AES_CBC_PAD (0x1085UL) -#define CKM_BLOWFISH_KEY_GEN (0x1090UL) -#define CKM_BLOWFISH_CBC (0x1091UL) -#define CKM_TWOFISH_KEY_GEN (0x1092UL) -#define CKM_TWOFISH_CBC (0x1093UL) -#define CKM_DES_ECB_ENCRYPT_DATA (0x1100UL) -#define CKM_DES_CBC_ENCRYPT_DATA (0x1101UL) -#define CKM_DES3_ECB_ENCRYPT_DATA (0x1102UL) -#define CKM_DES3_CBC_ENCRYPT_DATA (0x1103UL) -#define CKM_AES_ECB_ENCRYPT_DATA (0x1104UL) -#define CKM_AES_CBC_ENCRYPT_DATA (0x1105UL) -#define CKM_DSA_PARAMETER_GEN (0x2000UL) -#define CKM_DH_PKCS_PARAMETER_GEN (0x2001UL) -#define CKM_X9_42_DH_PARAMETER_GEN (0x2002UL) -#define CKM_VENDOR_DEFINED ((unsigned long) (1UL << 31)) - -/* Ammendments */ -#define CKM_SHA224 (0x255UL) -#define CKM_SHA224_HMAC (0x256UL) -#define CKM_SHA224_HMAC_GENERAL (0x257UL) -#define CKM_SHA224_RSA_PKCS (0x46UL) -#define CKM_SHA224_RSA_PKCS_PSS (0x47UL) -#define CKM_SHA224_KEY_DERIVATION (0x396UL) - -#define CKM_CAMELLIA_KEY_GEN (0x550UL) -#define CKM_CAMELLIA_ECB (0x551UL) -#define CKM_CAMELLIA_CBC (0x552UL) -#define CKM_CAMELLIA_MAC (0x553UL) -#define CKM_CAMELLIA_MAC_GENERAL (0x554UL) -#define CKM_CAMELLIA_CBC_PAD (0x555UL) -#define CKM_CAMELLIA_ECB_ENCRYPT_DATA (0x556UL) -#define CKM_CAMELLIA_CBC_ENCRYPT_DATA (0x557UL) - -struct ck_mechanism -{ - ck_mechanism_type_t mechanism; - void *parameter; - unsigned long parameter_len; -}; - - -struct ck_mechanism_info -{ - unsigned long min_key_size; - unsigned long max_key_size; - ck_flags_t flags; -}; - -#define CKF_HW (1UL << 0) -#define CKF_ENCRYPT (1UL << 8) -#define CKF_DECRYPT (1UL << 9) -#define CKF_DIGEST (1UL << 10) -#define CKF_SIGN (1UL << 11) -#define CKF_SIGN_RECOVER (1UL << 12) -#define CKF_VERIFY (1UL << 13) -#define CKF_VERIFY_RECOVER (1UL << 14) -#define CKF_GENERATE (1UL << 15) -#define CKF_GENERATE_KEY_PAIR (1UL << 16) -#define CKF_WRAP (1UL << 17) -#define CKF_UNWRAP (1UL << 18) -#define CKF_DERIVE (1UL << 19) -#define CKF_EXTENSION ((unsigned long) (1UL << 31)) - - -/* Flags for C_WaitForSlotEvent. */ -#define CKF_DONT_BLOCK (1UL) - - -typedef unsigned long ck_rv_t; - - -typedef ck_rv_t (*ck_notify_t) (ck_session_handle_t session, - ck_notification_t event, void *application); - -/* Forward reference. */ -struct ck_function_list; - -#define _CK_DECLARE_FUNCTION(name, args) \ -typedef ck_rv_t (*CK_ ## name) args; \ -ck_rv_t CK_SPEC name args - -_CK_DECLARE_FUNCTION (C_Initialize, (void *init_args)); -_CK_DECLARE_FUNCTION (C_Finalize, (void *reserved)); -_CK_DECLARE_FUNCTION (C_GetInfo, (struct ck_info *info)); -_CK_DECLARE_FUNCTION (C_GetFunctionList, - (struct ck_function_list **function_list)); - -_CK_DECLARE_FUNCTION (C_GetSlotList, - (unsigned char token_present, ck_slot_id_t *slot_list, - unsigned long *count)); -_CK_DECLARE_FUNCTION (C_GetSlotInfo, - (ck_slot_id_t slot_id, struct ck_slot_info *info)); -_CK_DECLARE_FUNCTION (C_GetTokenInfo, - (ck_slot_id_t slot_id, struct ck_token_info *info)); -_CK_DECLARE_FUNCTION (C_WaitForSlotEvent, - (ck_flags_t flags, ck_slot_id_t *slot, void *reserved)); -_CK_DECLARE_FUNCTION (C_GetMechanismList, - (ck_slot_id_t slot_id, - ck_mechanism_type_t *mechanism_list, - unsigned long *count)); -_CK_DECLARE_FUNCTION (C_GetMechanismInfo, - (ck_slot_id_t slot_id, ck_mechanism_type_t type, - struct ck_mechanism_info *info)); -_CK_DECLARE_FUNCTION (C_InitToken, - (ck_slot_id_t slot_id, unsigned char *pin, - unsigned long pin_len, unsigned char *label)); -_CK_DECLARE_FUNCTION (C_InitPIN, - (ck_session_handle_t session, unsigned char *pin, - unsigned long pin_len)); -_CK_DECLARE_FUNCTION (C_SetPIN, - (ck_session_handle_t session, unsigned char *old_pin, - unsigned long old_len, unsigned char *new_pin, - unsigned long new_len)); - -_CK_DECLARE_FUNCTION (C_OpenSession, - (ck_slot_id_t slot_id, ck_flags_t flags, - void *application, ck_notify_t notify, - ck_session_handle_t *session)); -_CK_DECLARE_FUNCTION (C_CloseSession, (ck_session_handle_t session)); -_CK_DECLARE_FUNCTION (C_CloseAllSessions, (ck_slot_id_t slot_id)); -_CK_DECLARE_FUNCTION (C_GetSessionInfo, - (ck_session_handle_t session, - struct ck_session_info *info)); -_CK_DECLARE_FUNCTION (C_GetOperationState, - (ck_session_handle_t session, - unsigned char *operation_state, - unsigned long *operation_state_len)); -_CK_DECLARE_FUNCTION (C_SetOperationState, - (ck_session_handle_t session, - unsigned char *operation_state, - unsigned long operation_state_len, - ck_object_handle_t encryption_key, - ck_object_handle_t authentiation_key)); -_CK_DECLARE_FUNCTION (C_Login, - (ck_session_handle_t session, ck_user_type_t user_type, - unsigned char *pin, unsigned long pin_len)); -_CK_DECLARE_FUNCTION (C_Logout, (ck_session_handle_t session)); - -_CK_DECLARE_FUNCTION (C_CreateObject, - (ck_session_handle_t session, - struct ck_attribute *templ, - unsigned long count, ck_object_handle_t *object)); -_CK_DECLARE_FUNCTION (C_CopyObject, - (ck_session_handle_t session, ck_object_handle_t object, - struct ck_attribute *templ, unsigned long count, - ck_object_handle_t *new_object)); -_CK_DECLARE_FUNCTION (C_DestroyObject, - (ck_session_handle_t session, - ck_object_handle_t object)); -_CK_DECLARE_FUNCTION (C_GetObjectSize, - (ck_session_handle_t session, - ck_object_handle_t object, - unsigned long *size)); -_CK_DECLARE_FUNCTION (C_GetAttributeValue, - (ck_session_handle_t session, - ck_object_handle_t object, - struct ck_attribute *templ, - unsigned long count)); -_CK_DECLARE_FUNCTION (C_SetAttributeValue, - (ck_session_handle_t session, - ck_object_handle_t object, - struct ck_attribute *templ, - unsigned long count)); -_CK_DECLARE_FUNCTION (C_FindObjectsInit, - (ck_session_handle_t session, - struct ck_attribute *templ, - unsigned long count)); -_CK_DECLARE_FUNCTION (C_FindObjects, - (ck_session_handle_t session, - ck_object_handle_t *object, - unsigned long max_object_count, - unsigned long *object_count)); -_CK_DECLARE_FUNCTION (C_FindObjectsFinal, - (ck_session_handle_t session)); - -_CK_DECLARE_FUNCTION (C_EncryptInit, - (ck_session_handle_t session, - struct ck_mechanism *mechanism, - ck_object_handle_t key)); -_CK_DECLARE_FUNCTION (C_Encrypt, - (ck_session_handle_t session, - unsigned char *data, unsigned long data_len, - unsigned char *encrypted_data, - unsigned long *encrypted_data_len)); -_CK_DECLARE_FUNCTION (C_EncryptUpdate, - (ck_session_handle_t session, - unsigned char *part, unsigned long part_len, - unsigned char *encrypted_part, - unsigned long *encrypted_part_len)); -_CK_DECLARE_FUNCTION (C_EncryptFinal, - (ck_session_handle_t session, - unsigned char *last_encrypted_part, - unsigned long *last_encrypted_part_len)); - -_CK_DECLARE_FUNCTION (C_DecryptInit, - (ck_session_handle_t session, - struct ck_mechanism *mechanism, - ck_object_handle_t key)); -_CK_DECLARE_FUNCTION (C_Decrypt, - (ck_session_handle_t session, - unsigned char *encrypted_data, - unsigned long encrypted_data_len, - unsigned char *data, unsigned long *data_len)); -_CK_DECLARE_FUNCTION (C_DecryptUpdate, - (ck_session_handle_t session, - unsigned char *encrypted_part, - unsigned long encrypted_part_len, - unsigned char *part, unsigned long *part_len)); -_CK_DECLARE_FUNCTION (C_DecryptFinal, - (ck_session_handle_t session, - unsigned char *last_part, - unsigned long *last_part_len)); - -_CK_DECLARE_FUNCTION (C_DigestInit, - (ck_session_handle_t session, - struct ck_mechanism *mechanism)); -_CK_DECLARE_FUNCTION (C_Digest, - (ck_session_handle_t session, - unsigned char *data, unsigned long data_len, - unsigned char *digest, - unsigned long *digest_len)); -_CK_DECLARE_FUNCTION (C_DigestUpdate, - (ck_session_handle_t session, - unsigned char *part, unsigned long part_len)); -_CK_DECLARE_FUNCTION (C_DigestKey, - (ck_session_handle_t session, ck_object_handle_t key)); -_CK_DECLARE_FUNCTION (C_DigestFinal, - (ck_session_handle_t session, - unsigned char *digest, - unsigned long *digest_len)); - -_CK_DECLARE_FUNCTION (C_SignInit, - (ck_session_handle_t session, - struct ck_mechanism *mechanism, - ck_object_handle_t key)); -_CK_DECLARE_FUNCTION (C_Sign, - (ck_session_handle_t session, - unsigned char *data, unsigned long data_len, - unsigned char *signature, - unsigned long *signature_len)); -_CK_DECLARE_FUNCTION (C_SignUpdate, - (ck_session_handle_t session, - unsigned char *part, unsigned long part_len)); -_CK_DECLARE_FUNCTION (C_SignFinal, - (ck_session_handle_t session, - unsigned char *signature, - unsigned long *signature_len)); -_CK_DECLARE_FUNCTION (C_SignRecoverInit, - (ck_session_handle_t session, - struct ck_mechanism *mechanism, - ck_object_handle_t key)); -_CK_DECLARE_FUNCTION (C_SignRecover, - (ck_session_handle_t session, - unsigned char *data, unsigned long data_len, - unsigned char *signature, - unsigned long *signature_len)); - -_CK_DECLARE_FUNCTION (C_VerifyInit, - (ck_session_handle_t session, - struct ck_mechanism *mechanism, - ck_object_handle_t key)); -_CK_DECLARE_FUNCTION (C_Verify, - (ck_session_handle_t session, - unsigned char *data, unsigned long data_len, - unsigned char *signature, - unsigned long signature_len)); -_CK_DECLARE_FUNCTION (C_VerifyUpdate, - (ck_session_handle_t session, - unsigned char *part, unsigned long part_len)); -_CK_DECLARE_FUNCTION (C_VerifyFinal, - (ck_session_handle_t session, - unsigned char *signature, - unsigned long signature_len)); -_CK_DECLARE_FUNCTION (C_VerifyRecoverInit, - (ck_session_handle_t session, - struct ck_mechanism *mechanism, - ck_object_handle_t key)); -_CK_DECLARE_FUNCTION (C_VerifyRecover, - (ck_session_handle_t session, - unsigned char *signature, - unsigned long signature_len, - unsigned char *data, - unsigned long *data_len)); - -_CK_DECLARE_FUNCTION (C_DigestEncryptUpdate, - (ck_session_handle_t session, - unsigned char *part, unsigned long part_len, - unsigned char *encrypted_part, - unsigned long *encrypted_part_len)); -_CK_DECLARE_FUNCTION (C_DecryptDigestUpdate, - (ck_session_handle_t session, - unsigned char *encrypted_part, - unsigned long encrypted_part_len, - unsigned char *part, - unsigned long *part_len)); -_CK_DECLARE_FUNCTION (C_SignEncryptUpdate, - (ck_session_handle_t session, - unsigned char *part, unsigned long part_len, - unsigned char *encrypted_part, - unsigned long *encrypted_part_len)); -_CK_DECLARE_FUNCTION (C_DecryptVerifyUpdate, - (ck_session_handle_t session, - unsigned char *encrypted_part, - unsigned long encrypted_part_len, - unsigned char *part, - unsigned long *part_len)); - -_CK_DECLARE_FUNCTION (C_GenerateKey, - (ck_session_handle_t session, - struct ck_mechanism *mechanism, - struct ck_attribute *templ, - unsigned long count, - ck_object_handle_t *key)); -_CK_DECLARE_FUNCTION (C_GenerateKeyPair, - (ck_session_handle_t session, - struct ck_mechanism *mechanism, - struct ck_attribute *public_key_template, - unsigned long public_key_attribute_count, - struct ck_attribute *private_key_template, - unsigned long private_key_attribute_count, - ck_object_handle_t *public_key, - ck_object_handle_t *private_key)); -_CK_DECLARE_FUNCTION (C_WrapKey, - (ck_session_handle_t session, - struct ck_mechanism *mechanism, - ck_object_handle_t wrapping_key, - ck_object_handle_t key, - unsigned char *wrapped_key, - unsigned long *wrapped_key_len)); -_CK_DECLARE_FUNCTION (C_UnwrapKey, - (ck_session_handle_t session, - struct ck_mechanism *mechanism, - ck_object_handle_t unwrapping_key, - unsigned char *wrapped_key, - unsigned long wrapped_key_len, - struct ck_attribute *templ, - unsigned long attribute_count, - ck_object_handle_t *key)); -_CK_DECLARE_FUNCTION (C_DeriveKey, - (ck_session_handle_t session, - struct ck_mechanism *mechanism, - ck_object_handle_t base_key, - struct ck_attribute *templ, - unsigned long attribute_count, - ck_object_handle_t *key)); - -_CK_DECLARE_FUNCTION (C_SeedRandom, - (ck_session_handle_t session, unsigned char *seed, - unsigned long seed_len)); -_CK_DECLARE_FUNCTION (C_GenerateRandom, - (ck_session_handle_t session, - unsigned char *random_data, - unsigned long random_len)); - -_CK_DECLARE_FUNCTION (C_GetFunctionStatus, (ck_session_handle_t session)); -_CK_DECLARE_FUNCTION (C_CancelFunction, (ck_session_handle_t session)); - - -struct ck_function_list -{ - struct ck_version version; - CK_C_Initialize C_Initialize; - CK_C_Finalize C_Finalize; - CK_C_GetInfo C_GetInfo; - CK_C_GetFunctionList C_GetFunctionList; - CK_C_GetSlotList C_GetSlotList; - CK_C_GetSlotInfo C_GetSlotInfo; - CK_C_GetTokenInfo C_GetTokenInfo; - CK_C_GetMechanismList C_GetMechanismList; - CK_C_GetMechanismInfo C_GetMechanismInfo; - CK_C_InitToken C_InitToken; - CK_C_InitPIN C_InitPIN; - CK_C_SetPIN C_SetPIN; - CK_C_OpenSession C_OpenSession; - CK_C_CloseSession C_CloseSession; - CK_C_CloseAllSessions C_CloseAllSessions; - CK_C_GetSessionInfo C_GetSessionInfo; - CK_C_GetOperationState C_GetOperationState; - CK_C_SetOperationState C_SetOperationState; - CK_C_Login C_Login; - CK_C_Logout C_Logout; - CK_C_CreateObject C_CreateObject; - CK_C_CopyObject C_CopyObject; - CK_C_DestroyObject C_DestroyObject; - CK_C_GetObjectSize C_GetObjectSize; - CK_C_GetAttributeValue C_GetAttributeValue; - CK_C_SetAttributeValue C_SetAttributeValue; - CK_C_FindObjectsInit C_FindObjectsInit; - CK_C_FindObjects C_FindObjects; - CK_C_FindObjectsFinal C_FindObjectsFinal; - CK_C_EncryptInit C_EncryptInit; - CK_C_Encrypt C_Encrypt; - CK_C_EncryptUpdate C_EncryptUpdate; - CK_C_EncryptFinal C_EncryptFinal; - CK_C_DecryptInit C_DecryptInit; - CK_C_Decrypt C_Decrypt; - CK_C_DecryptUpdate C_DecryptUpdate; - CK_C_DecryptFinal C_DecryptFinal; - CK_C_DigestInit C_DigestInit; - CK_C_Digest C_Digest; - CK_C_DigestUpdate C_DigestUpdate; - CK_C_DigestKey C_DigestKey; - CK_C_DigestFinal C_DigestFinal; - CK_C_SignInit C_SignInit; - CK_C_Sign C_Sign; - CK_C_SignUpdate C_SignUpdate; - CK_C_SignFinal C_SignFinal; - CK_C_SignRecoverInit C_SignRecoverInit; - CK_C_SignRecover C_SignRecover; - CK_C_VerifyInit C_VerifyInit; - CK_C_Verify C_Verify; - CK_C_VerifyUpdate C_VerifyUpdate; - CK_C_VerifyFinal C_VerifyFinal; - CK_C_VerifyRecoverInit C_VerifyRecoverInit; - CK_C_VerifyRecover C_VerifyRecover; - CK_C_DigestEncryptUpdate C_DigestEncryptUpdate; - CK_C_DecryptDigestUpdate C_DecryptDigestUpdate; - CK_C_SignEncryptUpdate C_SignEncryptUpdate; - CK_C_DecryptVerifyUpdate C_DecryptVerifyUpdate; - CK_C_GenerateKey C_GenerateKey; - CK_C_GenerateKeyPair C_GenerateKeyPair; - CK_C_WrapKey C_WrapKey; - CK_C_UnwrapKey C_UnwrapKey; - CK_C_DeriveKey C_DeriveKey; - CK_C_SeedRandom C_SeedRandom; - CK_C_GenerateRandom C_GenerateRandom; - CK_C_GetFunctionStatus C_GetFunctionStatus; - CK_C_CancelFunction C_CancelFunction; - CK_C_WaitForSlotEvent C_WaitForSlotEvent; -}; - - -typedef ck_rv_t (*ck_createmutex_t) (void **mutex); -typedef ck_rv_t (*ck_destroymutex_t) (void *mutex); -typedef ck_rv_t (*ck_lockmutex_t) (void *mutex); -typedef ck_rv_t (*ck_unlockmutex_t) (void *mutex); - - -struct ck_c_initialize_args -{ - ck_createmutex_t create_mutex; - ck_destroymutex_t destroy_mutex; - ck_lockmutex_t lock_mutex; - ck_unlockmutex_t unlock_mutex; - ck_flags_t flags; - void *reserved; -}; - - -#define CKF_LIBRARY_CANT_CREATE_OS_THREADS (1UL << 0) -#define CKF_OS_LOCKING_OK (1UL << 1) - -#define CKR_OK (0UL) -#define CKR_CANCEL (1UL) -#define CKR_HOST_MEMORY (2UL) -#define CKR_SLOT_ID_INVALID (3UL) -#define CKR_GENERAL_ERROR (5UL) -#define CKR_FUNCTION_FAILED (6UL) -#define CKR_ARGUMENTS_BAD (7UL) -#define CKR_NO_EVENT (8UL) -#define CKR_NEED_TO_CREATE_THREADS (9UL) -#define CKR_CANT_LOCK (0xaUL) -#define CKR_ATTRIBUTE_READ_ONLY (0x10UL) -#define CKR_ATTRIBUTE_SENSITIVE (0x11UL) -#define CKR_ATTRIBUTE_TYPE_INVALID (0x12UL) -#define CKR_ATTRIBUTE_VALUE_INVALID (0x13UL) -#define CKR_DATA_INVALID (0x20UL) -#define CKR_DATA_LEN_RANGE (0x21UL) -#define CKR_DEVICE_ERROR (0x30UL) -#define CKR_DEVICE_MEMORY (0x31UL) -#define CKR_DEVICE_REMOVED (0x32UL) -#define CKR_ENCRYPTED_DATA_INVALID (0x40UL) -#define CKR_ENCRYPTED_DATA_LEN_RANGE (0x41UL) -#define CKR_FUNCTION_CANCELED (0x50UL) -#define CKR_FUNCTION_NOT_PARALLEL (0x51UL) -#define CKR_FUNCTION_NOT_SUPPORTED (0x54UL) -#define CKR_KEY_HANDLE_INVALID (0x60UL) -#define CKR_KEY_SIZE_RANGE (0x62UL) -#define CKR_KEY_TYPE_INCONSISTENT (0x63UL) -#define CKR_KEY_NOT_NEEDED (0x64UL) -#define CKR_KEY_CHANGED (0x65UL) -#define CKR_KEY_NEEDED (0x66UL) -#define CKR_KEY_INDIGESTIBLE (0x67UL) -#define CKR_KEY_FUNCTION_NOT_PERMITTED (0x68UL) -#define CKR_KEY_NOT_WRAPPABLE (0x69UL) -#define CKR_KEY_UNEXTRACTABLE (0x6aUL) -#define CKR_MECHANISM_INVALID (0x70UL) -#define CKR_MECHANISM_PARAM_INVALID (0x71UL) -#define CKR_OBJECT_HANDLE_INVALID (0x82UL) -#define CKR_OPERATION_ACTIVE (0x90UL) -#define CKR_OPERATION_NOT_INITIALIZED (0x91UL) -#define CKR_PIN_INCORRECT (0xa0UL) -#define CKR_PIN_INVALID (0xa1UL) -#define CKR_PIN_LEN_RANGE (0xa2UL) -#define CKR_PIN_EXPIRED (0xa3UL) -#define CKR_PIN_LOCKED (0xa4UL) -#define CKR_SESSION_CLOSED (0xb0UL) -#define CKR_SESSION_COUNT (0xb1UL) -#define CKR_SESSION_HANDLE_INVALID (0xb3UL) -#define CKR_SESSION_PARALLEL_NOT_SUPPORTED (0xb4UL) -#define CKR_SESSION_READ_ONLY (0xb5UL) -#define CKR_SESSION_EXISTS (0xb6UL) -#define CKR_SESSION_READ_ONLY_EXISTS (0xb7UL) -#define CKR_SESSION_READ_WRITE_SO_EXISTS (0xb8UL) -#define CKR_SIGNATURE_INVALID (0xc0UL) -#define CKR_SIGNATURE_LEN_RANGE (0xc1UL) -#define CKR_TEMPLATE_INCOMPLETE (0xd0UL) -#define CKR_TEMPLATE_INCONSISTENT (0xd1UL) -#define CKR_TOKEN_NOT_PRESENT (0xe0UL) -#define CKR_TOKEN_NOT_RECOGNIZED (0xe1UL) -#define CKR_TOKEN_WRITE_PROTECTED (0xe2UL) -#define CKR_UNWRAPPING_KEY_HANDLE_INVALID (0xf0UL) -#define CKR_UNWRAPPING_KEY_SIZE_RANGE (0xf1UL) -#define CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT (0xf2UL) -#define CKR_USER_ALREADY_LOGGED_IN (0x100UL) -#define CKR_USER_NOT_LOGGED_IN (0x101UL) -#define CKR_USER_PIN_NOT_INITIALIZED (0x102UL) -#define CKR_USER_TYPE_INVALID (0x103UL) -#define CKR_USER_ANOTHER_ALREADY_LOGGED_IN (0x104UL) -#define CKR_USER_TOO_MANY_TYPES (0x105UL) -#define CKR_WRAPPED_KEY_INVALID (0x110UL) -#define CKR_WRAPPED_KEY_LEN_RANGE (0x112UL) -#define CKR_WRAPPING_KEY_HANDLE_INVALID (0x113UL) -#define CKR_WRAPPING_KEY_SIZE_RANGE (0x114UL) -#define CKR_WRAPPING_KEY_TYPE_INCONSISTENT (0x115UL) -#define CKR_RANDOM_SEED_NOT_SUPPORTED (0x120UL) -#define CKR_RANDOM_NO_RNG (0x121UL) -#define CKR_DOMAIN_PARAMS_INVALID (0x130UL) -#define CKR_BUFFER_TOO_SMALL (0x150UL) -#define CKR_SAVED_STATE_INVALID (0x160UL) -#define CKR_INFORMATION_SENSITIVE (0x170UL) -#define CKR_STATE_UNSAVEABLE (0x180UL) -#define CKR_CRYPTOKI_NOT_INITIALIZED (0x190UL) -#define CKR_CRYPTOKI_ALREADY_INITIALIZED (0x191UL) -#define CKR_MUTEX_BAD (0x1a0UL) -#define CKR_MUTEX_NOT_LOCKED (0x1a1UL) -#define CKR_FUNCTION_REJECTED (0x200UL) -#define CKR_VENDOR_DEFINED ((unsigned long) (1UL << 31)) - - - -/* Compatibility layer. */ - -#ifdef CRYPTOKI_COMPAT - -#undef CK_DEFINE_FUNCTION -#define CK_DEFINE_FUNCTION(retval, name) retval CK_SPEC name - -/* For NULL. */ -#include <stddef.h> - -typedef unsigned char CK_BYTE; -typedef unsigned char CK_CHAR; -typedef unsigned char CK_UTF8CHAR; -typedef unsigned char CK_BBOOL; -typedef unsigned long int CK_ULONG; -typedef long int CK_LONG; -typedef CK_BYTE *CK_BYTE_PTR; -typedef CK_CHAR *CK_CHAR_PTR; -typedef CK_UTF8CHAR *CK_UTF8CHAR_PTR; -typedef CK_ULONG *CK_ULONG_PTR; -typedef void *CK_VOID_PTR; -typedef void **CK_VOID_PTR_PTR; -#define CK_FALSE 0 -#define CK_TRUE 1 -#ifndef CK_DISABLE_TRUE_FALSE -#ifndef FALSE -#define FALSE 0 -#endif -#ifndef TRUE -#define TRUE 1 -#endif -#endif - -typedef struct ck_version CK_VERSION; -typedef struct ck_version *CK_VERSION_PTR; - -typedef struct ck_info CK_INFO; -typedef struct ck_info *CK_INFO_PTR; - -typedef ck_slot_id_t *CK_SLOT_ID_PTR; - -typedef struct ck_slot_info CK_SLOT_INFO; -typedef struct ck_slot_info *CK_SLOT_INFO_PTR; - -typedef struct ck_token_info CK_TOKEN_INFO; -typedef struct ck_token_info *CK_TOKEN_INFO_PTR; - -typedef ck_session_handle_t *CK_SESSION_HANDLE_PTR; - -typedef struct ck_session_info CK_SESSION_INFO; -typedef struct ck_session_info *CK_SESSION_INFO_PTR; - -typedef ck_object_handle_t *CK_OBJECT_HANDLE_PTR; - -typedef ck_object_class_t *CK_OBJECT_CLASS_PTR; - -typedef struct ck_attribute CK_ATTRIBUTE; -typedef struct ck_attribute *CK_ATTRIBUTE_PTR; - -typedef struct ck_date CK_DATE; -typedef struct ck_date *CK_DATE_PTR; - -typedef ck_mechanism_type_t *CK_MECHANISM_TYPE_PTR; - -typedef struct ck_mechanism CK_MECHANISM; -typedef struct ck_mechanism *CK_MECHANISM_PTR; - -typedef struct ck_mechanism_info CK_MECHANISM_INFO; -typedef struct ck_mechanism_info *CK_MECHANISM_INFO_PTR; - -typedef struct ck_function_list CK_FUNCTION_LIST; -typedef struct ck_function_list *CK_FUNCTION_LIST_PTR; -typedef struct ck_function_list **CK_FUNCTION_LIST_PTR_PTR; - -typedef struct ck_c_initialize_args CK_C_INITIALIZE_ARGS; -typedef struct ck_c_initialize_args *CK_C_INITIALIZE_ARGS_PTR; - -#define NULL_PTR NULL - -/* Delete the helper macros defined at the top of the file. */ -#undef ck_flags_t -#undef ck_version - -#undef ck_info -#undef cryptoki_version -#undef manufacturer_id -#undef library_description -#undef library_version - -#undef ck_notification_t -#undef ck_slot_id_t - -#undef ck_slot_info -#undef slot_description -#undef hardware_version -#undef firmware_version - -#undef ck_token_info -#undef serial_number -#undef max_session_count -#undef session_count -#undef max_rw_session_count -#undef rw_session_count -#undef max_pin_len -#undef min_pin_len -#undef total_public_memory -#undef free_public_memory -#undef total_private_memory -#undef free_private_memory -#undef utc_time - -#undef ck_session_handle_t -#undef ck_user_type_t -#undef ck_state_t - -#undef ck_session_info -#undef slot_id -#undef device_error - -#undef ck_object_handle_t -#undef ck_object_class_t -#undef ck_hw_feature_type_t -#undef ck_key_type_t -#undef ck_certificate_type_t -#undef ck_attribute_type_t - -#undef ck_attribute -#undef value -#undef value_len - -#undef ck_date - -#undef ck_mechanism_type_t - -#undef ck_mechanism -#undef parameter -#undef parameter_len - -#undef ck_mechanism_info -#undef min_key_size -#undef max_key_size - -#undef ck_rv_t -#undef ck_notify_t - -#undef ck_function_list - -#undef ck_createmutex_t -#undef ck_destroymutex_t -#undef ck_lockmutex_t -#undef ck_unlockmutex_t - -#undef ck_c_initialize_args -#undef create_mutex -#undef destroy_mutex -#undef lock_mutex -#undef unlock_mutex -#undef reserved - -#endif /* CRYPTOKI_COMPAT */ - - -/* System dependencies. */ -#if defined(_WIN32) || defined(CRYPTOKI_FORCE_WIN32) -#pragma pack(pop, cryptoki) -#endif - -#if defined(__cplusplus) -} -#endif - -#endif /* PKCS11_H */ diff --git a/common/pkcs11i.h b/common/pkcs11i.h deleted file mode 100644 index d9e3ffc..0000000 --- a/common/pkcs11i.h +++ /dev/null @@ -1,505 +0,0 @@ -/* - * 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@redhat.com> - */ - -#ifndef PKCS11_I_H_ -#define PKCS11_I_H_ 1 - -#if defined(__cplusplus) -extern "C" { -#endif - -/* ------------------------------------------------------------------- - * TRUST ASSERTIONS - * - * These are retired and should not be used in new code - */ - -#define CKO_X_TRUST_ASSERTION (CKO_X_VENDOR + 100) -#define CKA_X_ASSERTION_TYPE (CKA_X_VENDOR + 1) -#define CKA_X_CERTIFICATE_VALUE (CKA_X_VENDOR + 2) -#define CKA_X_PURPOSE (CKA_X_VENDOR + 3) -#define CKA_X_PEER (CKA_X_VENDOR + 4) -typedef CK_ULONG CK_X_ASSERTION_TYPE; -#define CKT_X_DISTRUSTED_CERTIFICATE 1UL -#define CKT_X_PINNED_CERTIFICATE 2UL -#define CKT_X_ANCHORED_CERTIFICATE 3UL - -/* ------------------------------------------------------------------- - * Other deprecated definitions - */ -#define CKA_X_CRITICAL (CKA_X_VENDOR + 101) - -/* ------------------------------------------------------------------- - * SUBCLASSABLE PKCS#11 FUNCTIONS - */ - -typedef struct _CK_X_FUNCTION_LIST CK_X_FUNCTION_LIST; - -typedef CK_RV (* CK_X_Initialize) (CK_X_FUNCTION_LIST *, - CK_VOID_PTR); - -typedef CK_RV (* CK_X_Finalize) (CK_X_FUNCTION_LIST *, - CK_VOID_PTR); - -typedef CK_RV (* CK_X_GetInfo) (CK_X_FUNCTION_LIST *, - CK_INFO_PTR); - -typedef CK_RV (* CK_X_GetSlotList) (CK_X_FUNCTION_LIST *, - CK_BBOOL, - CK_SLOT_ID_PTR, - CK_ULONG_PTR); - -typedef CK_RV (* CK_X_GetSlotInfo) (CK_X_FUNCTION_LIST *, - CK_SLOT_ID, - CK_SLOT_INFO_PTR); - -typedef CK_RV (* CK_X_GetTokenInfo) (CK_X_FUNCTION_LIST *, - CK_SLOT_ID, - CK_TOKEN_INFO_PTR); - -typedef CK_RV (* CK_X_GetMechanismList) (CK_X_FUNCTION_LIST *, - CK_SLOT_ID, - CK_MECHANISM_TYPE_PTR, - CK_ULONG_PTR); - -typedef CK_RV (* CK_X_GetMechanismInfo) (CK_X_FUNCTION_LIST *, - CK_SLOT_ID, - CK_MECHANISM_TYPE, - CK_MECHANISM_INFO_PTR); - -typedef CK_RV (* CK_X_InitToken) (CK_X_FUNCTION_LIST *, - CK_SLOT_ID, - CK_BYTE_PTR, - CK_ULONG, - CK_BYTE_PTR); - -typedef CK_RV (* CK_X_InitPIN) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG); - -typedef CK_RV (* CK_X_SetPIN) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG, - CK_BYTE_PTR, - CK_ULONG); - -typedef CK_RV (* CK_X_OpenSession) (CK_X_FUNCTION_LIST *, - CK_SLOT_ID, - CK_FLAGS, - CK_VOID_PTR, - CK_NOTIFY, - CK_SESSION_HANDLE_PTR); - -typedef CK_RV (* CK_X_CloseSession) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE); - -typedef CK_RV (* CK_X_CloseAllSessions) (CK_X_FUNCTION_LIST *, - CK_SLOT_ID); - -typedef CK_RV (* CK_X_GetSessionInfo) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_SESSION_INFO_PTR); - -typedef CK_RV (* CK_X_GetOperationState) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG_PTR); - -typedef CK_RV (* CK_X_SetOperationState) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG, - CK_OBJECT_HANDLE, - CK_OBJECT_HANDLE); - -typedef CK_RV (* CK_X_Login) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_USER_TYPE, - CK_BYTE_PTR, - CK_ULONG); - -typedef CK_RV (* CK_X_Logout) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE); - -typedef CK_RV (* CK_X_CreateObject) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_ATTRIBUTE_PTR, - CK_ULONG, - CK_OBJECT_HANDLE_PTR); - -typedef CK_RV (* CK_X_CopyObject) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_OBJECT_HANDLE, - CK_ATTRIBUTE_PTR, - CK_ULONG, - CK_OBJECT_HANDLE_PTR); - -typedef CK_RV (* CK_X_DestroyObject) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_OBJECT_HANDLE); - -typedef CK_RV (* CK_X_GetObjectSize) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_OBJECT_HANDLE, - CK_ULONG_PTR); - -typedef CK_RV (* CK_X_GetAttributeValue) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_OBJECT_HANDLE, - CK_ATTRIBUTE_PTR, - CK_ULONG); - -typedef CK_RV (* CK_X_SetAttributeValue) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_OBJECT_HANDLE, - CK_ATTRIBUTE_PTR, - CK_ULONG); - -typedef CK_RV (* CK_X_FindObjectsInit) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_ATTRIBUTE_PTR, - CK_ULONG); - -typedef CK_RV (* CK_X_FindObjects) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_OBJECT_HANDLE_PTR, - CK_ULONG, - CK_ULONG_PTR); - -typedef CK_RV (* CK_X_FindObjectsFinal) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE); - -typedef CK_RV (* CK_X_EncryptInit) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_MECHANISM_PTR, - CK_OBJECT_HANDLE); - -typedef CK_RV (* CK_X_Encrypt) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG, - CK_BYTE_PTR, - CK_ULONG_PTR); - -typedef CK_RV (* CK_X_EncryptUpdate) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG, - CK_BYTE_PTR, - CK_ULONG_PTR); - -typedef CK_RV (* CK_X_EncryptFinal) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG_PTR); - -typedef CK_RV (* CK_X_DecryptInit) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_MECHANISM_PTR, - CK_OBJECT_HANDLE); - -typedef CK_RV (* CK_X_Decrypt) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG, - CK_BYTE_PTR, - CK_ULONG_PTR); - -typedef CK_RV (* CK_X_DecryptUpdate) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG, - CK_BYTE_PTR, - CK_ULONG_PTR); - -typedef CK_RV (* CK_X_DecryptFinal) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG_PTR); - -typedef CK_RV (* CK_X_DigestInit) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_MECHANISM_PTR); - -typedef CK_RV (* CK_X_Digest) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG, - CK_BYTE_PTR, - CK_ULONG_PTR); - -typedef CK_RV (* CK_X_DigestUpdate) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG); - -typedef CK_RV (* CK_X_DigestKey) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_OBJECT_HANDLE); - -typedef CK_RV (* CK_X_DigestFinal) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG_PTR); - -typedef CK_RV (* CK_X_SignInit) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_MECHANISM_PTR, - CK_OBJECT_HANDLE); - -typedef CK_RV (* CK_X_Sign) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG, - CK_BYTE_PTR, - CK_ULONG_PTR); - -typedef CK_RV (* CK_X_SignUpdate) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG); - -typedef CK_RV (* CK_X_SignFinal) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG_PTR); - -typedef CK_RV (* CK_X_SignRecoverInit) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_MECHANISM_PTR, - CK_OBJECT_HANDLE); - -typedef CK_RV (* CK_X_SignRecover) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG, - CK_BYTE_PTR, - CK_ULONG_PTR); - -typedef CK_RV (* CK_X_VerifyInit) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_MECHANISM_PTR, - CK_OBJECT_HANDLE); - -typedef CK_RV (* CK_X_Verify) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG, - CK_BYTE_PTR, - CK_ULONG); - -typedef CK_RV (* CK_X_VerifyUpdate) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG); - -typedef CK_RV (* CK_X_VerifyFinal) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG); - -typedef CK_RV (* CK_X_VerifyRecoverInit) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_MECHANISM_PTR, - CK_OBJECT_HANDLE); - -typedef CK_RV (* CK_X_VerifyRecover) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG, - CK_BYTE_PTR, - CK_ULONG_PTR); - -typedef CK_RV (* CK_X_DigestEncryptUpdate) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG, - CK_BYTE_PTR, - CK_ULONG_PTR); - -typedef CK_RV (* CK_X_DecryptDigestUpdate) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG, - CK_BYTE_PTR, - CK_ULONG_PTR); - -typedef CK_RV (* CK_X_SignEncryptUpdate) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG, - CK_BYTE_PTR, - CK_ULONG_PTR); - -typedef CK_RV (* CK_X_DecryptVerifyUpdate) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG, - CK_BYTE_PTR, - CK_ULONG_PTR); - -typedef CK_RV (* CK_X_GenerateKey) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_MECHANISM_PTR, - CK_ATTRIBUTE_PTR, - CK_ULONG, - CK_OBJECT_HANDLE_PTR); - -typedef CK_RV (* CK_X_GenerateKeyPair) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_MECHANISM_PTR, - CK_ATTRIBUTE_PTR, - CK_ULONG, - CK_ATTRIBUTE_PTR, - CK_ULONG, - CK_OBJECT_HANDLE_PTR, - CK_OBJECT_HANDLE_PTR); - -typedef CK_RV (* CK_X_WrapKey) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_MECHANISM_PTR, - CK_OBJECT_HANDLE, - CK_OBJECT_HANDLE, - CK_BYTE_PTR, - CK_ULONG_PTR); - -typedef CK_RV (* CK_X_UnwrapKey) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_MECHANISM_PTR, - CK_OBJECT_HANDLE, - CK_BYTE_PTR, - CK_ULONG, - CK_ATTRIBUTE_PTR, - CK_ULONG, - CK_OBJECT_HANDLE_PTR); - -typedef CK_RV (* CK_X_DeriveKey) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_MECHANISM_PTR, - CK_OBJECT_HANDLE, - CK_ATTRIBUTE_PTR, - CK_ULONG, - CK_OBJECT_HANDLE_PTR); - -typedef CK_RV (* CK_X_SeedRandom) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG); - -typedef CK_RV (* CK_X_GenerateRandom) (CK_X_FUNCTION_LIST *, - CK_SESSION_HANDLE, - CK_BYTE_PTR, - CK_ULONG); - -typedef CK_RV (* CK_X_WaitForSlotEvent) (CK_X_FUNCTION_LIST *, - CK_FLAGS, - CK_SLOT_ID_PTR, - CK_VOID_PTR); - -struct _CK_X_FUNCTION_LIST { - CK_VERSION version; - CK_X_Initialize C_Initialize; - CK_X_Finalize C_Finalize; - CK_X_GetInfo C_GetInfo; - CK_X_GetSlotList C_GetSlotList; - CK_X_GetSlotInfo C_GetSlotInfo; - CK_X_GetTokenInfo C_GetTokenInfo; - CK_X_GetMechanismList C_GetMechanismList; - CK_X_GetMechanismInfo C_GetMechanismInfo; - CK_X_InitToken C_InitToken; - CK_X_InitPIN C_InitPIN; - CK_X_SetPIN C_SetPIN; - CK_X_OpenSession C_OpenSession; - CK_X_CloseSession C_CloseSession; - CK_X_CloseAllSessions C_CloseAllSessions; - CK_X_GetSessionInfo C_GetSessionInfo; - CK_X_GetOperationState C_GetOperationState; - CK_X_SetOperationState C_SetOperationState; - CK_X_Login C_Login; - CK_X_Logout C_Logout; - CK_X_CreateObject C_CreateObject; - CK_X_CopyObject C_CopyObject; - CK_X_DestroyObject C_DestroyObject; - CK_X_GetObjectSize C_GetObjectSize; - CK_X_GetAttributeValue C_GetAttributeValue; - CK_X_SetAttributeValue C_SetAttributeValue; - CK_X_FindObjectsInit C_FindObjectsInit; - CK_X_FindObjects C_FindObjects; - CK_X_FindObjectsFinal C_FindObjectsFinal; - CK_X_EncryptInit C_EncryptInit; - CK_X_Encrypt C_Encrypt; - CK_X_EncryptUpdate C_EncryptUpdate; - CK_X_EncryptFinal C_EncryptFinal; - CK_X_DecryptInit C_DecryptInit; - CK_X_Decrypt C_Decrypt; - CK_X_DecryptUpdate C_DecryptUpdate; - CK_X_DecryptFinal C_DecryptFinal; - CK_X_DigestInit C_DigestInit; - CK_X_Digest C_Digest; - CK_X_DigestUpdate C_DigestUpdate; - CK_X_DigestKey C_DigestKey; - CK_X_DigestFinal C_DigestFinal; - CK_X_SignInit C_SignInit; - CK_X_Sign C_Sign; - CK_X_SignUpdate C_SignUpdate; - CK_X_SignFinal C_SignFinal; - CK_X_SignRecoverInit C_SignRecoverInit; - CK_X_SignRecover C_SignRecover; - CK_X_VerifyInit C_VerifyInit; - CK_X_Verify C_Verify; - CK_X_VerifyUpdate C_VerifyUpdate; - CK_X_VerifyFinal C_VerifyFinal; - CK_X_VerifyRecoverInit C_VerifyRecoverInit; - CK_X_VerifyRecover C_VerifyRecover; - CK_X_DigestEncryptUpdate C_DigestEncryptUpdate; - CK_X_DecryptDigestUpdate C_DecryptDigestUpdate; - CK_X_SignEncryptUpdate C_SignEncryptUpdate; - CK_X_DecryptVerifyUpdate C_DecryptVerifyUpdate; - CK_X_GenerateKey C_GenerateKey; - CK_X_GenerateKeyPair C_GenerateKeyPair; - CK_X_WrapKey C_WrapKey; - CK_X_UnwrapKey C_UnwrapKey; - CK_X_DeriveKey C_DeriveKey; - CK_X_SeedRandom C_SeedRandom; - CK_X_GenerateRandom C_GenerateRandom; - CK_X_WaitForSlotEvent C_WaitForSlotEvent; -}; - -#if defined(__cplusplus) -} -#endif - -#endif /* PKCS11_X_H_ */ diff --git a/common/pkcs11x.h b/common/pkcs11x.h deleted file mode 100644 index 4a89f73..0000000 --- a/common/pkcs11x.h +++ /dev/null @@ -1,149 +0,0 @@ -/* - * 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@redhat.com> - */ - -#ifndef PKCS11_X_H_ -#define PKCS11_X_H_ 1 - -#if defined(__cplusplus) -extern "C" { -#endif - -/* ------------------------------------------------------------------- - * NSS TRUST OBJECTS - * - * And related, non-standard - */ - -/* Define this if you want the NSS specific symbols */ -#define CRYPTOKI_NSS_VENDOR_DEFINED 1 -#ifdef CRYPTOKI_NSS_VENDOR_DEFINED - -/* Various NSS objects */ -#define CKO_NSS_CRL 0xce534351UL -#define CKO_NSS_SMIME 0xce534352UL -#define CKO_NSS_TRUST 0xce534353UL -#define CKO_NSS_BUILTIN_ROOT_LIST 0xce534354UL -#define CKO_NSS_NEWSLOT 0xce534355UL -#define CKO_NSS_DELSLOT 0xce534356UL - -/* Various NSS key types */ -#define CKK_NSS_PKCS8 0xce534351UL - -/* Various NSS attributes */ -#define CKA_NSS_URL 0xce534351UL -#define CKA_NSS_EMAIL 0xce534352UL -#define CKA_NSS_SMIME_INFO 0xce534353UL -#define CKA_NSS_SMIME_TIMESTAMP 0xce534354UL -#define CKA_NSS_PKCS8_SALT 0xce534355UL -#define CKA_NSS_PASSWORD_CHECK 0xce534356UL -#define CKA_NSS_EXPIRES 0xce534357UL -#define CKA_NSS_KRL 0xce534358UL -#define CKA_NSS_PQG_COUNTER 0xce534364UL -#define CKA_NSS_PQG_SEED 0xce534365UL -#define CKA_NSS_PQG_H 0xce534366UL -#define CKA_NSS_PQG_SEED_BITS 0xce534367UL -#define CKA_NSS_MODULE_SPEC 0xce534368UL - -/* NSS trust attributes */ -#define CKA_TRUST_DIGITAL_SIGNATURE 0xce536351UL -#define CKA_TRUST_NON_REPUDIATION 0xce536352UL -#define CKA_TRUST_KEY_ENCIPHERMENT 0xce536353UL -#define CKA_TRUST_DATA_ENCIPHERMENT 0xce536354UL -#define CKA_TRUST_KEY_AGREEMENT 0xce536355UL -#define CKA_TRUST_KEY_CERT_SIGN 0xce536356UL -#define CKA_TRUST_CRL_SIGN 0xce536357UL -#define CKA_TRUST_SERVER_AUTH 0xce536358UL -#define CKA_TRUST_CLIENT_AUTH 0xce536359UL -#define CKA_TRUST_CODE_SIGNING 0xce53635aUL -#define CKA_TRUST_EMAIL_PROTECTION 0xce53635bUL -#define CKA_TRUST_IPSEC_END_SYSTEM 0xce53635cUL -#define CKA_TRUST_IPSEC_TUNNEL 0xce53635dUL -#define CKA_TRUST_IPSEC_USER 0xce53635eUL -#define CKA_TRUST_TIME_STAMPING 0xce53635fUL -#define CKA_TRUST_STEP_UP_APPROVED 0xce536360UL -#define CKA_CERT_SHA1_HASH 0xce5363b4UL -#define CKA_CERT_MD5_HASH 0xce5363b5UL - -/* NSS trust values */ -typedef CK_ULONG CK_TRUST; -#define CKT_NSS_TRUSTED 0xce534351UL -#define CKT_NSS_TRUSTED_DELEGATOR 0xce534352UL -#define CKT_NSS_MUST_VERIFY_TRUST 0xce534353UL -#define CKT_NSS_NOT_TRUSTED 0xce53435AUL -#define CKT_NSS_TRUST_UNKNOWN 0xce534355UL -#define CKT_NSS_VALID_DELEGATOR 0xce53435BUL - -/* NSS specific mechanisms */ -#define CKM_NSS_AES_KEY_WRAP 0xce534351UL -#define CKM_NSS_AES_KEY_WRAP_PAD 0xce534352UL - -/* NSS specific return values */ -#define CKR_NSS_CERTDB_FAILED 0xce534351UL -#define CKR_NSS_KEYDB_FAILED 0xce534352UL - -#endif /* CRYPTOKI_NSS_VENDOR_DEFINED */ - -/* Define this if you want the vendor specific symbols */ -#define CRYPTOKI_X_VENDOR_DEFINED 1 -#ifdef CRYPTOKI_X_VENDOR_DEFINED - -#define CKA_X_VENDOR (CKA_VENDOR_DEFINED | 0x58444700UL) -#define CKO_X_VENDOR (CKA_VENDOR_DEFINED | 0x58444700UL) - -/* ------------------------------------------------------------------- - * BLACKLISTS - */ - -#define CKA_X_DISTRUSTED (CKA_X_VENDOR + 100) - -/* ------------------------------------------------------------------- - * CERTIFICATE EXTENSIONS - * - * For attaching certificate extensions to certificates - */ - -#define CKO_X_CERTIFICATE_EXTENSION (CKO_X_VENDOR + 200) - -/* From the 2.40 draft */ -#ifndef CKA_PUBLIC_KEY_INFO -#define CKA_PUBLIC_KEY_INFO 0x00000129UL -#endif - -#endif /* CRYPTOKI_X_VENDOR_DEFINED */ - -#if defined(__cplusplus) -} -#endif - -#endif /* PKCS11_X_H_ */ diff --git a/common/test-array.c b/common/test-array.c deleted file mode 100644 index 695917a..0000000 --- a/common/test-array.c +++ /dev/null @@ -1,209 +0,0 @@ -/* - * 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> - */ - -#include "config.h" - -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#include "array.h" -#include "test.h" - -static void -test_create (void) -{ - p11_array *array; - - array = p11_array_new (NULL); - assert_ptr_not_null (array); - p11_array_free (array); -} - -static void -test_free_null (void) -{ - p11_array_free (NULL); -} - -static void -destroy_value (void *data) -{ - int *value = data; - *value = 2; -} - -static void -test_free_destroys (void) -{ - p11_array *array; - int value = 0; - - array = p11_array_new (destroy_value); - assert_ptr_not_null (array); - if (!p11_array_push (array, &value)) - assert_not_reached (); - p11_array_free (array); - - assert_num_eq (2, value); -} - -static void -test_add (void) -{ - char *value = "VALUE"; - p11_array *array; - - array = p11_array_new (NULL); - if (!p11_array_push (array, value)) - assert_not_reached (); - - assert_num_eq (1, array->num); - assert_ptr_eq (array->elem[0], value); - - p11_array_free (array); -} - -static void -test_add_remove (void) -{ - char *value = "VALUE"; - p11_array *array; - - array = p11_array_new (NULL); - if (!p11_array_push (array, value)) - assert_not_reached (); - - assert_num_eq (1, array->num); - - assert_ptr_eq (array->elem[0], value); - - p11_array_remove (array, 0); - - assert_num_eq (0, array->num); - - p11_array_free (array); -} - -static void -test_remove_destroys (void) -{ - p11_array *array; - int value = 0; - - array = p11_array_new (destroy_value); - if (!p11_array_push (array, &value)) - assert_not_reached (); - - p11_array_remove (array, 0); - - assert_num_eq (2, value); - - /* should not be destroyed again */ - value = 0; - - p11_array_free (array); - - assert_num_eq (0, value); -} - -static void -test_remove_and_count (void) -{ - p11_array *array; - int *value; - int i; - - array = p11_array_new (free); - - assert_num_eq (0, array->num); - - for (i = 0; i < 20000; ++i) { - value = malloc (sizeof (int)); - assert (value != NULL); - *value = i; - if (!p11_array_push (array, value)) - assert_not_reached (); - assert_num_eq (i + 1, array->num); - } - - for (i = 10; i < 20000; ++i) { - p11_array_remove (array, 10); - assert_num_eq (20010 - (i + 1), array->num); - } - - assert_num_eq (10, array->num); - - p11_array_free (array); -} - -static void -test_clear_destroys (void) -{ - p11_array *array; - int value = 0; - - array = p11_array_new (destroy_value); - if (!p11_array_push (array, &value)) - assert_not_reached (); - - assert_num_eq (1, array->num); - - p11_array_clear (array); - - assert_num_eq (2, value); - assert_num_eq (0, array->num); - - /* should not be destroyed again */ - value = 0; - - p11_array_free (array); - - assert_num_eq (0, value); -} - -int -main (int argc, - char *argv[]) -{ - p11_test (test_create, "/array/create"); - p11_test (test_add, "/array/add"); - p11_test (test_add_remove, "/array/add-remove"); - p11_test (test_remove_destroys, "/array/remove-destroys"); - p11_test (test_remove_and_count, "/array/remove-and-count"); - p11_test (test_free_null, "/array/free-null"); - p11_test (test_free_destroys, "/array/free-destroys"); - p11_test (test_clear_destroys, "/array/clear-destroys"); - return p11_test_run (argc, argv); -} diff --git a/common/test-attrs.c b/common/test-attrs.c deleted file mode 100644 index 79895e2..0000000 --- a/common/test-attrs.c +++ /dev/null @@ -1,757 +0,0 @@ -/* - * 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 "test.h" - -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#include "attrs.h" -#include "debug.h" - -static void -test_terminator (void) -{ - CK_ATTRIBUTE attrs[] = { - { CKA_LABEL, "label", 5 }, - { CKA_LABEL, NULL, 0 }, - { CKA_INVALID }, - }; - - assert_num_eq (true, p11_attrs_terminator (attrs + 2)); - assert_num_eq (true, p11_attrs_terminator (NULL)); - assert_num_eq (false, p11_attrs_terminator (attrs)); - assert_num_eq (false, p11_attrs_terminator (attrs + 1)); -} - -static void -test_count (void) -{ - CK_BBOOL vtrue = CK_TRUE; - - CK_ATTRIBUTE attrs[] = { - { CKA_LABEL, "label", 5 }, - { CKA_TOKEN, &vtrue, sizeof (vtrue) }, - { CKA_INVALID }, - }; - - CK_ATTRIBUTE empty[] = { - { CKA_INVALID }, - }; - - assert_num_eq (2, p11_attrs_count (attrs)); - assert_num_eq (0, p11_attrs_count (NULL)); - assert_num_eq (0, p11_attrs_count (empty)); -} - -static void -test_build_one (void) -{ - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE add = { CKA_LABEL, "yay", 3 }; - - attrs = p11_attrs_build (NULL, &add, NULL); - - /* Test the first attribute */ - assert_ptr_not_null (attrs); - assert (attrs->type == CKA_LABEL); - assert_num_eq (3, attrs->ulValueLen); - assert (memcmp (attrs->pValue, "yay", 3) == 0); - - assert (attrs[1].type == CKA_INVALID); - - p11_attrs_free (attrs); -} - -static void -test_build_two (void) -{ - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE one = { CKA_LABEL, "yay", 3 }; - CK_ATTRIBUTE two = { CKA_VALUE, "eight", 5 }; - - attrs = p11_attrs_build (NULL, &one, &two, NULL); - - assert_ptr_not_null (attrs); - assert (attrs[0].type == CKA_LABEL); - assert_num_eq (3, attrs[0].ulValueLen); - assert (memcmp (attrs[0].pValue, "yay", 3) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[1].type == CKA_VALUE); - assert_num_eq (5, attrs[1].ulValueLen); - assert (memcmp (attrs[1].pValue, "eight", 5) == 0); - - assert (attrs[2].type == CKA_INVALID); - - p11_attrs_free (attrs); -} - -static void -test_build_invalid (void) -{ - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE one = { CKA_LABEL, "yay", 3 }; - CK_ATTRIBUTE invalid = { CKA_INVALID }; - CK_ATTRIBUTE two = { CKA_VALUE, "eight", 5 }; - - attrs = p11_attrs_build (NULL, &one, &invalid, &two, NULL); - - assert_ptr_not_null (attrs); - assert (attrs[0].type == CKA_LABEL); - assert_num_eq (3, attrs[0].ulValueLen); - assert (memcmp (attrs[0].pValue, "yay", 3) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[1].type == CKA_VALUE); - assert_num_eq (5, attrs[1].ulValueLen); - assert (memcmp (attrs[1].pValue, "eight", 5) == 0); - - assert (attrs[2].type == CKA_INVALID); - - p11_attrs_free (attrs); -} - -static void -test_buildn_two (void) -{ - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE add[] = { - { CKA_LABEL, "yay", 3 }, - { CKA_VALUE, "eight", 5 } - }; - - attrs = p11_attrs_buildn (NULL, add, 2); - - /* Test the first attribute */ - assert_ptr_not_null (attrs); - assert (attrs->type == CKA_LABEL); - assert_num_eq (3, attrs->ulValueLen); - assert (memcmp (attrs->pValue, "yay", 3) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[1].type == CKA_VALUE); - assert_num_eq (5, attrs[1].ulValueLen); - assert (memcmp (attrs[1].pValue, "eight", 5) == 0); - - assert (attrs[2].type == CKA_INVALID); - - p11_attrs_free (attrs); -} - -static void -test_buildn_one (void) -{ - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE add = { CKA_LABEL, "yay", 3 }; - - attrs = p11_attrs_buildn (NULL, &add, 1); - - /* Test the first attribute */ - assert_ptr_not_null (attrs); - assert (attrs->type == CKA_LABEL); - assert_num_eq (3, attrs->ulValueLen); - assert (memcmp (attrs->pValue, "yay", 3) == 0); - - assert (attrs[1].type == CKA_INVALID); - - p11_attrs_free (attrs); -} - -static void -test_build_add (void) -{ - CK_ATTRIBUTE initial[] = { - { CKA_LABEL, "label", 5 }, - { CKA_VALUE, "nine", 4 }, - }; - - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE one = { CKA_LABEL, "yay", 3 }; - CK_ATTRIBUTE two = { CKA_TOKEN, "\x01", 1 }; - - attrs = p11_attrs_buildn (NULL, initial, 2); - attrs = p11_attrs_build (attrs, &one, &two, NULL); - - assert_ptr_not_null (attrs); - assert (attrs[0].type == CKA_LABEL); - assert_num_eq (3, attrs[0].ulValueLen); - assert (memcmp (attrs[0].pValue, "yay", 3) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[1].type == CKA_VALUE); - assert_num_eq (4, attrs[1].ulValueLen); - assert (memcmp (attrs[1].pValue, "nine", 4) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[2].type == CKA_TOKEN); - assert_num_eq (1, attrs[2].ulValueLen); - assert (memcmp (attrs[2].pValue, "\x01", 1) == 0); - - assert (attrs[3].type == CKA_INVALID); - - p11_attrs_free (attrs); -} - -static void -test_build_null (void) -{ - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE add = { CKA_LABEL, NULL, (CK_ULONG)-1 }; - - attrs = p11_attrs_build (NULL, &add, NULL); - - /* Test the first attribute */ - assert_ptr_not_null (attrs); - assert (attrs->type == CKA_LABEL); - assert (attrs->ulValueLen == (CK_ULONG)-1); - assert_ptr_eq (NULL, attrs->pValue); - - p11_attrs_free (attrs); -} - -static void -test_dup (void) -{ - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE original[] = { - { CKA_LABEL, "yay", 3 }, - { CKA_VALUE, "eight", 5 }, - { CKA_INVALID } - }; - - attrs = p11_attrs_dup (original); - - /* Test the first attribute */ - assert_ptr_not_null (attrs); - assert (attrs->type == CKA_LABEL); - assert_num_eq (3, attrs->ulValueLen); - assert (memcmp (attrs->pValue, "yay", 3) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[1].type == CKA_VALUE); - assert_num_eq (5, attrs[1].ulValueLen); - assert (memcmp (attrs[1].pValue, "eight", 5) == 0); - - assert (attrs[2].type == CKA_INVALID); - - p11_attrs_free (attrs); -} - -static void -test_take (void) -{ - CK_ATTRIBUTE initial[] = { - { CKA_LABEL, "label", 5 }, - { CKA_VALUE, "nine", 4 }, - }; - - CK_ATTRIBUTE *attrs; - - attrs = p11_attrs_buildn (NULL, initial, 2); - attrs = p11_attrs_take (attrs, CKA_LABEL, strdup ("boooyah"), 7); - attrs = p11_attrs_take (attrs, CKA_TOKEN, strdup ("\x01"), 1); - assert_ptr_not_null (attrs); - - assert (attrs[0].type == CKA_LABEL); - assert_num_eq (7, attrs[0].ulValueLen); - assert (memcmp (attrs[0].pValue, "boooyah", 7) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[1].type == CKA_VALUE); - assert_num_eq (4, attrs[1].ulValueLen); - assert (memcmp (attrs[1].pValue, "nine", 4) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[2].type == CKA_TOKEN); - assert_num_eq (1, attrs[2].ulValueLen); - assert (memcmp (attrs[2].pValue, "\x01", 1) == 0); - - assert (attrs[3].type == CKA_INVALID); - - p11_attrs_free (attrs); -} - - -static void -test_merge_replace (void) -{ - CK_ATTRIBUTE initial[] = { - { CKA_LABEL, "label", 5 }, - { CKA_VALUE, "nine", 4 }, - }; - - CK_ATTRIBUTE extra[] = { - { CKA_LABEL, "boooyah", 7 }, - { CKA_APPLICATION, "disco", 5 }, - }; - - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE *merge; - - attrs = p11_attrs_buildn (NULL, initial, 2); - merge = p11_attrs_buildn (NULL, extra, 2); - attrs = p11_attrs_merge (attrs, merge, true); - assert_ptr_not_null (attrs); - - assert (attrs[0].type == CKA_LABEL); - assert_num_eq (7, attrs[0].ulValueLen); - assert (memcmp (attrs[0].pValue, "boooyah", 7) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[1].type == CKA_VALUE); - assert_num_eq (4, attrs[1].ulValueLen); - assert (memcmp (attrs[1].pValue, "nine", 4) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[2].type == CKA_APPLICATION); - assert_num_eq (5, attrs[2].ulValueLen); - assert (memcmp (attrs[2].pValue, "disco", 5) == 0); - - assert (attrs[3].type == CKA_INVALID); - - p11_attrs_free (attrs); -} - -static void -test_merge_empty (void) -{ - CK_ATTRIBUTE extra[] = { - { CKA_LABEL, "boooyah", 7 }, - { CKA_APPLICATION, "disco", 5 }, - }; - - CK_ATTRIBUTE *attrs = NULL; - CK_ATTRIBUTE *merge; - - merge = p11_attrs_buildn (NULL, extra, 2); - attrs = p11_attrs_merge (attrs, merge, true); - assert_ptr_not_null (attrs); - assert_ptr_eq (merge, attrs); - - p11_attrs_free (attrs); -} - -static void -test_merge_augment (void) -{ - CK_ATTRIBUTE initial[] = { - { CKA_LABEL, "label", 5 }, - { CKA_VALUE, "nine", 4 }, - }; - - CK_ATTRIBUTE extra[] = { - { CKA_LABEL, "boooyah", 7 }, - { CKA_APPLICATION, "disco", 5 }, - }; - - CK_ATTRIBUTE *attrs; - CK_ATTRIBUTE *merge; - - attrs = p11_attrs_buildn (NULL, initial, 2); - merge = p11_attrs_buildn (NULL, extra, 2); - attrs = p11_attrs_merge (attrs, merge, false); - assert_ptr_not_null (attrs); - - assert (attrs[0].type == CKA_LABEL); - assert_num_eq (5, attrs[0].ulValueLen); - assert (memcmp (attrs[0].pValue, "label", 5) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[1].type == CKA_VALUE); - assert_num_eq (4, attrs[1].ulValueLen); - assert (memcmp (attrs[1].pValue, "nine", 4) == 0); - - assert_ptr_not_null (attrs); - assert (attrs[2].type == CKA_APPLICATION); - assert_num_eq (5, attrs[2].ulValueLen); - assert (memcmp (attrs[2].pValue, "disco", 5) == 0); - - assert (attrs[3].type == CKA_INVALID); - - p11_attrs_free (attrs); -} - -static void -test_free_null (void) -{ - p11_attrs_free (NULL); -} - -static void -test_equal (void) -{ - char *data = "extra attribute"; - CK_ATTRIBUTE one = { CKA_LABEL, "yay", 3 }; - CK_ATTRIBUTE null = { CKA_LABEL, NULL, 3 }; - CK_ATTRIBUTE two = { CKA_VALUE, "yay", 3 }; - CK_ATTRIBUTE other = { CKA_VALUE, data, 5 }; - CK_ATTRIBUTE overflow = { CKA_VALUE, data, 5 }; - CK_ATTRIBUTE content = { CKA_VALUE, "conte", 5 }; - - assert (p11_attr_equal (&one, &one)); - assert (!p11_attr_equal (&one, NULL)); - assert (!p11_attr_equal (NULL, &one)); - assert (!p11_attr_equal (&one, &two)); - assert (!p11_attr_equal (&two, &other)); - assert (p11_attr_equal (&other, &overflow)); - assert (!p11_attr_equal (&one, &null)); - assert (!p11_attr_equal (&one, &null)); - assert (!p11_attr_equal (&other, &content)); -} - -static void -test_hash (void) -{ - char *data = "extra attribute"; - CK_ATTRIBUTE one = { CKA_LABEL, "yay", 3 }; - CK_ATTRIBUTE null = { CKA_LABEL, NULL, 3 }; - CK_ATTRIBUTE two = { CKA_VALUE, "yay", 3 }; - CK_ATTRIBUTE other = { CKA_VALUE, data, 5 }; - CK_ATTRIBUTE overflow = { CKA_VALUE, data, 5 }; - CK_ATTRIBUTE content = { CKA_VALUE, "conte", 5 }; - unsigned int hash; - - hash = p11_attr_hash (&one); - assert (hash != 0); - - assert (p11_attr_hash (&one) == hash); - assert (p11_attr_hash (&two) != hash); - assert (p11_attr_hash (&other) != hash); - assert (p11_attr_hash (&overflow) != hash); - assert (p11_attr_hash (&null) != hash); - assert (p11_attr_hash (&content) != hash); - - hash = p11_attr_hash (NULL); - assert (hash == 0); -} - -static void -test_to_string (void) -{ - char *data = "extra attribute"; - CK_ATTRIBUTE one = { CKA_LABEL, "yay", 3 }; - CK_ATTRIBUTE attrs[] = { - { CKA_LABEL, "yay", 3 }, - { CKA_VALUE, data, 5 }, - { CKA_INVALID }, - }; - - char *string; - - - string = p11_attr_to_string (&one, CKA_INVALID); - assert_str_eq ("{ CKA_LABEL = (3) \"yay\" }", string); - free (string); - - string = p11_attrs_to_string (attrs, -1); - assert_str_eq ("(2) [ { CKA_LABEL = (3) \"yay\" }, { CKA_VALUE = (5) NOT-PRINTED } ]", string); - free (string); - - string = p11_attrs_to_string (attrs, 1); - assert_str_eq ("(1) [ { CKA_LABEL = (3) \"yay\" } ]", string); - free (string); -} - -static void -test_find (void) -{ - CK_BBOOL vtrue = CK_TRUE; - CK_ATTRIBUTE *attr; - - CK_ATTRIBUTE attrs[] = { - { CKA_LABEL, "label", 5 }, - { CKA_TOKEN, &vtrue, sizeof (vtrue) }, - { CKA_INVALID }, - }; - - attr = p11_attrs_find (attrs, CKA_LABEL); - assert_ptr_eq (attrs + 0, attr); - - attr = p11_attrs_find (attrs, CKA_TOKEN); - assert_ptr_eq (attrs + 1, attr); - - attr = p11_attrs_find (attrs, CKA_VALUE); - assert_ptr_eq (NULL, attr); -} - -static void -test_findn (void) -{ - CK_BBOOL vtrue = CK_TRUE; - CK_ATTRIBUTE *attr; - - CK_ATTRIBUTE attrs[] = { - { CKA_LABEL, "label", 5 }, - { CKA_TOKEN, &vtrue, sizeof (vtrue) }, - }; - - attr = p11_attrs_findn (attrs, 2, CKA_LABEL); - assert_ptr_eq (attrs + 0, attr); - - attr = p11_attrs_findn (attrs, 2, CKA_TOKEN); - assert_ptr_eq (attrs + 1, attr); - - attr = p11_attrs_findn (attrs, 2, CKA_VALUE); - assert_ptr_eq (NULL, attr); - - attr = p11_attrs_findn (attrs, 1, CKA_TOKEN); - assert_ptr_eq (NULL, attr); -} - -static void -test_remove (void) -{ - CK_BBOOL vtrue = CK_TRUE; - CK_ATTRIBUTE *attr; - CK_ATTRIBUTE *attrs; - CK_BBOOL ret; - - CK_ATTRIBUTE initial[] = { - { CKA_LABEL, "label", 5 }, - { CKA_TOKEN, &vtrue, sizeof (vtrue) }, - }; - - attrs = p11_attrs_buildn (NULL, initial, 2); - assert_ptr_not_null (attrs); - - attr = p11_attrs_find (attrs, CKA_LABEL); - assert_ptr_eq (attrs + 0, attr); - - ret = p11_attrs_remove (attrs, CKA_LABEL); - assert_num_eq (CK_TRUE, ret); - - attr = p11_attrs_find (attrs, CKA_LABEL); - assert_ptr_eq (NULL, attr); - - ret = p11_attrs_remove (attrs, CKA_LABEL); - assert_num_eq (CK_FALSE, ret); - - p11_attrs_free (attrs); -} - -static void -test_match (void) -{ - CK_BBOOL vtrue = CK_TRUE; - - CK_ATTRIBUTE attrs[] = { - { CKA_LABEL, "label", 5 }, - { CKA_TOKEN, &vtrue, sizeof (vtrue) }, - { CKA_INVALID }, - }; - - CK_ATTRIBUTE subset[] = { - { CKA_LABEL, "label", 5 }, - { CKA_INVALID }, - }; - - CK_ATTRIBUTE different[] = { - { CKA_LABEL, "other", 5 }, - { CKA_INVALID }, - }; - - CK_ATTRIBUTE extra[] = { - { CKA_VALUE, "the value", 9 }, - { CKA_LABEL, "other", 5 }, - { CKA_TOKEN, &vtrue, sizeof (vtrue) }, - { CKA_INVALID }, - }; - - assert (p11_attrs_match (attrs, attrs)); - assert (p11_attrs_match (attrs, subset)); - assert (!p11_attrs_match (attrs, different)); - assert (!p11_attrs_match (attrs, extra)); -} - -static void -test_matchn (void) -{ - CK_BBOOL vtrue = CK_TRUE; - - CK_ATTRIBUTE attrs[] = { - { CKA_LABEL, "label", 5 }, - { CKA_TOKEN, &vtrue, sizeof (vtrue) }, - { CKA_INVALID }, - }; - - CK_ATTRIBUTE subset[] = { - { CKA_LABEL, "label", 5 }, - }; - - CK_ATTRIBUTE different[] = { - { CKA_TOKEN, &vtrue, sizeof (vtrue) }, - { CKA_LABEL, "other", 5 }, - }; - - CK_ATTRIBUTE extra[] = { - { CKA_VALUE, "the value", 9 }, - { CKA_LABEL, "other", 5 }, - { CKA_TOKEN, &vtrue, sizeof (vtrue) }, - }; - - assert (p11_attrs_matchn (attrs, subset, 1)); - assert (!p11_attrs_matchn (attrs, different, 2)); - assert (!p11_attrs_matchn (attrs, extra, 3)); -} - -static void -test_find_bool (void) -{ - CK_BBOOL vtrue = CK_TRUE; - CK_BBOOL vfalse = CK_FALSE; - CK_BBOOL value; - - CK_ATTRIBUTE attrs[] = { - { CKA_LABEL, "\x01yy", 3 }, - { CKA_VALUE, &vtrue, (CK_ULONG)-1 }, - { CKA_TOKEN, &vtrue, sizeof (CK_BBOOL) }, - { CKA_TOKEN, &vfalse, sizeof (CK_BBOOL) }, - { CKA_INVALID }, - }; - - assert (p11_attrs_find_bool (attrs, CKA_TOKEN, &value) && value == CK_TRUE); - assert (!p11_attrs_find_bool (attrs, CKA_LABEL, &value)); - assert (!p11_attrs_find_bool (attrs, CKA_VALUE, &value)); -} - -static void -test_find_ulong (void) -{ - CK_ULONG v33 = 33UL; - CK_ULONG v45 = 45UL; - CK_ULONG value; - - CK_ATTRIBUTE attrs[] = { - { CKA_LABEL, &v33, 2 }, - { CKA_VALUE, &v45, (CK_ULONG)-1 }, - { CKA_BITS_PER_PIXEL, &v33, sizeof (CK_ULONG) }, - { CKA_BITS_PER_PIXEL, &v45, sizeof (CK_ULONG) }, - { CKA_INVALID }, - }; - - assert (p11_attrs_find_ulong (attrs, CKA_BITS_PER_PIXEL, &value) && value == v33); - assert (!p11_attrs_find_ulong (attrs, CKA_LABEL, &value)); - assert (!p11_attrs_find_ulong (attrs, CKA_VALUE, &value)); -} - -static void -test_find_value (void) -{ - void *value; - size_t length; - - CK_ATTRIBUTE attrs[] = { - { CKA_LABEL, "", (CK_ULONG)-1 }, - { CKA_LABEL, NULL, 5 }, - { CKA_LABEL, "", 0 }, - { CKA_LABEL, "test", 4 }, - { CKA_VALUE, NULL, 0 }, - { CKA_INVALID }, - }; - - value = p11_attrs_find_value (attrs, CKA_LABEL, &length); - assert_ptr_eq (attrs[3].pValue, value); - assert_num_eq (4, length); - - value = p11_attrs_find_value (attrs, CKA_LABEL, NULL); - assert_ptr_eq (attrs[3].pValue, value); - - value = p11_attrs_find_value (attrs, CKA_VALUE, &length); - assert_ptr_eq (NULL, value); - - value = p11_attrs_find_value (attrs, CKA_TOKEN, &length); - assert_ptr_eq (NULL, value); -} - -static void -test_find_valid (void) -{ - CK_ATTRIBUTE *attr; - - CK_ATTRIBUTE attrs[] = { - { CKA_LABEL, "", (CK_ULONG)-1 }, - { CKA_LABEL, NULL, 5 }, - { CKA_LABEL, "", 0 }, - { CKA_LABEL, "test", 4 }, - { CKA_VALUE, "value", 5 }, - { CKA_INVALID }, - }; - - attr = p11_attrs_find_valid (attrs, CKA_LABEL); - assert_ptr_eq (attrs + 3, attr); - - attr = p11_attrs_find_valid (attrs, CKA_VALUE); - assert_ptr_eq (attrs + 4, attr); - - attr = p11_attrs_find_valid (attrs, CKA_TOKEN); - assert_ptr_eq (NULL, attr); -} - -int -main (int argc, - char *argv[]) -{ - p11_test (test_equal, "/attrs/equal"); - p11_test (test_hash, "/attrs/hash"); - p11_test (test_to_string, "/attrs/to-string"); - - p11_test (test_terminator, "/attrs/terminator"); - p11_test (test_count, "/attrs/count"); - p11_test (test_build_one, "/attrs/build-one"); - p11_test (test_build_two, "/attrs/build-two"); - p11_test (test_build_invalid, "/attrs/build-invalid"); - p11_test (test_buildn_one, "/attrs/buildn-one"); - p11_test (test_buildn_two, "/attrs/buildn-two"); - p11_test (test_build_add, "/attrs/build-add"); - p11_test (test_build_null, "/attrs/build-null"); - p11_test (test_dup, "/attrs/dup"); - p11_test (test_take, "/attrs/take"); - p11_test (test_merge_replace, "/attrs/merge-replace"); - p11_test (test_merge_augment, "/attrs/merge-augment"); - p11_test (test_merge_empty, "/attrs/merge-empty"); - p11_test (test_free_null, "/attrs/free-null"); - p11_test (test_match, "/attrs/match"); - p11_test (test_matchn, "/attrs/matchn"); - p11_test (test_find, "/attrs/find"); - p11_test (test_findn, "/attrs/findn"); - p11_test (test_find_bool, "/attrs/find-bool"); - p11_test (test_find_ulong, "/attrs/find-ulong"); - p11_test (test_find_value, "/attrs/find-value"); - p11_test (test_find_valid, "/attrs/find-valid"); - p11_test (test_remove, "/attrs/remove"); - return p11_test_run (argc, argv); -} diff --git a/common/test-buffer.c b/common/test-buffer.c deleted file mode 100644 index 4fd060d..0000000 --- a/common/test-buffer.c +++ /dev/null @@ -1,199 +0,0 @@ -/* - * 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 <stef@thewalter.net> - */ - -#include "config.h" -#include "test.h" - -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#include "debug.h" -#include "buffer.h" - -static void -test_init_uninit (void) -{ - p11_buffer buffer; - - p11_buffer_init (&buffer, 10); - assert_ptr_not_null (buffer.data); - assert_num_eq (0, buffer.len); - assert_num_eq (0, buffer.flags); - assert (buffer.size >= 10); - assert_ptr_not_null (buffer.ffree); - assert_ptr_not_null (buffer.frealloc); - - p11_buffer_uninit (&buffer); -} - -static void -test_append (void) -{ - p11_buffer buffer; - - p11_buffer_init (&buffer, 10); - buffer.len = 5; - p11_buffer_append (&buffer, 35); - assert_num_eq (5 + 35, buffer.len); - assert (buffer.size >= 35 + 5); - - p11_buffer_append (&buffer, 15); - assert_num_eq (5 + 35 + 15, buffer.len); - assert (buffer.size >= 5 + 35 + 15); - - p11_buffer_uninit (&buffer); -} - -static void -test_null (void) -{ - p11_buffer buffer; - - p11_buffer_init_null (&buffer, 10); - p11_buffer_add (&buffer, "Blah", -1); - p11_buffer_add (&buffer, " blah", -1); - - assert_str_eq ("Blah blah", buffer.data); - - p11_buffer_uninit (&buffer); -} - -static int mock_realloced = 0; -static int mock_freed = 0; - -static void * -mock_realloc (void *data, - size_t size) -{ - mock_realloced++; - return realloc (data, size); -} - -static void -mock_free (void *data) -{ - mock_freed++; - free (data); -} - -static void -test_init_for_data (void) -{ - p11_buffer buffer; - unsigned char *ret; - size_t len; - - mock_realloced = 0; - mock_freed = 0; - - p11_buffer_init_full (&buffer, (unsigned char *)strdup ("blah"), 4, 0, - mock_realloc, mock_free); - - assert_ptr_not_null (buffer.data); - assert_str_eq ("blah", (char *)buffer.data); - assert_num_eq (4, buffer.len); - assert_num_eq (0, buffer.flags); - assert_num_eq (4, buffer.size); - assert_ptr_eq (mock_free, buffer.ffree); - assert_ptr_eq (mock_realloc, buffer.frealloc); - - assert_num_eq (0, mock_realloced); - assert_num_eq (0, mock_freed); - - len = buffer.len; - ret = p11_buffer_append (&buffer, 1024); - assert_ptr_eq ((char *)buffer.data + len, ret); - assert_num_eq (1, mock_realloced); - - p11_buffer_uninit (&buffer); - assert_num_eq (1, mock_realloced); - assert_num_eq (1, mock_freed); -} - -static void -test_steal (void) -{ - p11_buffer buffer; - char *string; - size_t length; - - mock_freed = 0; - - p11_buffer_init_full (&buffer, (unsigned char *)strdup ("blah"), 4, - P11_BUFFER_NULL, mock_realloc, mock_free); - - assert_ptr_not_null (buffer.data); - assert_str_eq ("blah", buffer.data); - - p11_buffer_add (&buffer, " yada", -1); - assert_str_eq ("blah yada", buffer.data); - - string = p11_buffer_steal (&buffer, &length); - p11_buffer_uninit (&buffer); - - assert_str_eq ("blah yada", string); - assert_num_eq (9, length); - assert_num_eq (0, mock_freed); - - free (string); -} - -static void -test_add (void) -{ - p11_buffer buffer; - - p11_buffer_init (&buffer, 10); - - p11_buffer_add (&buffer, (unsigned char *)"Planet Express", 15); - assert_num_eq (15, buffer.len); - assert_str_eq ("Planet Express", (char *)buffer.data); - assert (p11_buffer_ok (&buffer)); - - p11_buffer_uninit (&buffer); -} - -int -main (int argc, - char *argv[]) -{ - p11_test (test_init_uninit, "/buffer/init-uninit"); - p11_test (test_init_for_data, "/buffer/init-for-data"); - p11_test (test_append, "/buffer/append"); - p11_test (test_null, "/buffer/null"); - p11_test (test_add, "/buffer/add"); - p11_test (test_steal, "/buffer/steal"); - return p11_test_run (argc, argv); -} diff --git a/common/test-compat.c b/common/test-compat.c deleted file mode 100644 index e28698e..0000000 --- a/common/test-compat.c +++ /dev/null @@ -1,145 +0,0 @@ -/* - * Copyright (c) 2013 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@redhat.com> - */ - -#include "config.h" -#include "test.h" - -#include <errno.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#include "compat.h" - -static void -test_strndup (void) -{ - char unterminated[] = { 't', 'e', 's', 't', 'e', 'r', 'o', 'n', 'i', 'o' }; - char *res; - - res = strndup (unterminated, 6); - assert_str_eq (res, "tester"); - free (res); - - res = strndup ("test", 6); - assert_str_eq (res, "test"); - free (res); -} - -#ifdef OS_UNIX - -static void -test_getauxval (void) -{ - /* 23 is AT_SECURE */ - const char *args[] = { BUILDDIR "/frob-getauxval", "23", NULL }; - char *path; - int ret; - - ret = p11_test_run_child (args, true); - assert_num_eq (ret, 0); - - path = p11_test_copy_setgid (args[0]); - if (path == NULL) - return; - - args[0] = path; - ret = p11_test_run_child (args, true); - assert_num_cmp (ret, !=, 0); - - if (unlink (path) < 0) - assert_fail ("unlink failed", strerror (errno)); - free (path); -} - -static void -test_secure_getenv (void) -{ - const char *args[] = { BUILDDIR "/frob-getenv", "BLAH", NULL }; - char *path; - int ret; - - setenv ("BLAH", "5", 1); - - ret = p11_test_run_child (args, true); - assert_num_eq (ret, 5); - - path = p11_test_copy_setgid (args[0]); - if (path == NULL) - return; - - args[0] = path; - ret = p11_test_run_child (args, true); - assert_num_cmp (ret, ==, 0); - -/* if (unlink (path) < 0) - assert_fail ("unlink failed", strerror (errno)); - */ - free (path); -} - -static void -test_mmap (void) -{ - p11_mmap *map; - void *data; - size_t size; - char file[] = "emptyfileXXXXXX"; - int fd = mkstemp (file); - assert (fd >= 0); - close (fd); - /* mmap on empty file should work */ - map = p11_mmap_open (file, NULL, &data, &size); - unlink (file); - assert_ptr_not_null (map); - p11_mmap_close (map); -} - -#endif /* OS_UNIX */ - -int -main (int argc, - char *argv[]) -{ - p11_test (test_strndup, "/compat/strndup"); -#ifdef OS_UNIX - /* Don't run this test when under fakeroot */ - if (!getenv ("FAKED_MODE")) { - p11_test (test_getauxval, "/compat/getauxval"); - p11_test (test_secure_getenv, "/compat/secure_getenv"); - } - p11_test (test_mmap, "/compat/mmap"); -#endif - return p11_test_run (argc, argv); -} diff --git a/common/test-constants.c b/common/test-constants.c deleted file mode 100644 index 577d611..0000000 --- a/common/test-constants.c +++ /dev/null @@ -1,102 +0,0 @@ -/* - * 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 "test.h" - -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#include "attrs.h" -#include "constants.h" -#include "debug.h" - -static void -test_constants (void *arg) -{ - const p11_constant *constant = arg; - p11_dict *nicks, *names; - CK_ULONG check; - int i, j; - - nicks = p11_constant_reverse (true); - names = p11_constant_reverse (false); - - for (i = 1; constant[i].value != CKA_INVALID; i++) { - if (constant[i].value < constant[i - 1].value) - assert_fail ("attr constant out of order", constant[i].name); - } - for (i = 0; constant[i].value != CKA_INVALID; i++) { - assert_ptr_not_null (constant[i].name); - - if (constant[i].nicks[0]) { - assert_str_eq (constant[i].nicks[0], - p11_constant_nick (constant, constant[i].value)); - } - - assert_str_eq (constant[i].name, - p11_constant_name (constant, constant[i].value)); - - for (j = 0; constant[i].nicks[j] != NULL; j++) { - check = p11_constant_resolve (nicks, constant[i].nicks[j]); - assert_num_eq (constant[i].value, check); - } - - check = p11_constant_resolve (names, constant[i].name); - assert_num_eq (constant[i].value, check); - } - - p11_dict_free (names); - p11_dict_free (nicks); -} - -int -main (int argc, - char *argv[]) -{ - p11_testx (test_constants, (void *)p11_constant_types, "/constants/types"); - p11_testx (test_constants, (void *)p11_constant_classes, "/constants/classes"); - p11_testx (test_constants, (void *)p11_constant_trusts, "/constants/trusts"); - p11_testx (test_constants, (void *)p11_constant_certs, "/constants/certs"); - p11_testx (test_constants, (void *)p11_constant_keys, "/constants/keys"); - p11_testx (test_constants, (void *)p11_constant_asserts, "/constants/asserts"); - p11_testx (test_constants, (void *)p11_constant_categories, "/constants/categories"); - p11_testx (test_constants, (void *)p11_constant_mechanisms, "/constants/mechanisms"); - p11_testx (test_constants, (void *)p11_constant_users, "/constants/users"); - p11_testx (test_constants, (void *)p11_constant_states, "/constants/states"); - p11_testx (test_constants, (void *)p11_constant_returns, "/constants/returns"); - - return p11_test_run (argc, argv); -} diff --git a/common/test-dict.c b/common/test-dict.c deleted file mode 100644 index f12a34e..0000000 --- a/common/test-dict.c +++ /dev/null @@ -1,522 +0,0 @@ -/* - * 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> - */ - -#include "config.h" -#include "test.h" - -#include <assert.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#include "dict.h" - -static void -test_create (void) -{ - p11_dict *map; - - map = p11_dict_new (p11_dict_direct_hash, p11_dict_direct_equal, NULL, NULL); - assert_ptr_not_null (map); - p11_dict_free (map); -} - -static void -test_free_null (void) -{ - p11_dict_free (NULL); -} - -typedef struct { - int value; - bool freed; -} Key; - -static unsigned int -key_hash (const void *ptr) -{ - const Key *k = ptr; - assert (!k->freed); - return p11_dict_intptr_hash (&k->value); -} - -static bool -key_equal (const void *one, - const void *two) -{ - const Key *k1 = one; - const Key *k2 = two; - assert (!k1->freed); - assert (!k2->freed); - return p11_dict_intptr_equal (&k1->value, &k2->value); -} - -static void -key_destroy (void *data) -{ - Key *k = data; - assert (!k->freed); - k->freed = true; -} - -static void -value_destroy (void *data) -{ - int *value = data; - *value = 2; -} - -static void -test_free_destroys (void) -{ - p11_dict *map; - Key key = { 8, 0 }; - int value = 0; - - map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); - assert_ptr_not_null (map); - if (!p11_dict_set (map, &key, &value)) - assert_not_reached (); - p11_dict_free (map); - - assert_num_eq (true, key.freed); - assert_num_eq (2, value); -} - -static void -test_iterate (void) -{ - p11_dict *map; - p11_dictiter iter; - int key = 1; - int value = 2; - void *pkey; - void *pvalue; - int ret; - - map = p11_dict_new (p11_dict_direct_hash, p11_dict_direct_equal, NULL, NULL); - assert_ptr_not_null (map); - if (!p11_dict_set (map, &key, &value)) - assert_not_reached (); - - p11_dict_iterate (map, &iter); - - ret = p11_dict_next (&iter, &pkey, &pvalue); - assert_num_eq (1, ret); - assert_ptr_eq (pkey, &key); - assert_ptr_eq (pvalue, &value); - - ret = p11_dict_next (&iter, &pkey, &pvalue); - assert_num_eq (0, ret); - - p11_dict_free (map); -} - -static int -compar_strings (const void *one, - const void *two) -{ - char **p1 = (char **)one; - char **p2 = (char **)two; - return strcmp (*p1, *p2); -} - -static void -test_iterate_remove (void) -{ - p11_dict *map; - p11_dictiter iter; - char *keys[] = { "111", "222", "333" }; - char *values[] = { "444", "555", "666" }; - void *okeys[3]; - void *ovalues[3]; - bool ret; - int i; - - map = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, NULL, NULL); - assert_ptr_not_null (map); - - for (i = 0; i < 3; i++) { - if (!p11_dict_set (map, keys[i], values[i])) - assert_not_reached (); - } - - p11_dict_iterate (map, &iter); - - ret = p11_dict_next (&iter, &okeys[0], &ovalues[0]); - assert_num_eq (true, ret); - - ret = p11_dict_next (&iter, &okeys[1], &ovalues[1]); - assert_num_eq (true, ret); - if (!p11_dict_remove (map, okeys[1])) - assert_not_reached (); - - ret = p11_dict_next (&iter, &okeys[2], &ovalues[2]); - assert_num_eq (true, ret); - - ret = p11_dict_next (&iter, NULL, NULL); - assert_num_eq (false, ret); - - assert_num_eq (2, p11_dict_size (map)); - p11_dict_free (map); - - qsort (okeys, 3, sizeof (void *), compar_strings); - qsort (ovalues, 3, sizeof (void *), compar_strings); - - for (i = 0; i < 3; i++) { - assert_str_eq (keys[i], okeys[i]); - assert_ptr_eq (keys[i], okeys[i]); - assert_str_eq (values[i], ovalues[i]); - assert_ptr_eq (values[i], ovalues[i]); - } -} - -static void -test_set_get (void) -{ - char *key = "KEY"; - char *value = "VALUE"; - char *check; - p11_dict *map; - - map = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, NULL, NULL); - p11_dict_set (map, key, value); - check = p11_dict_get (map, key); - assert_ptr_eq (check, value); - - p11_dict_free (map); -} - -static void -test_set_get_remove (void) -{ - char *key = "KEY"; - char *value = "VALUE"; - char *check; - p11_dict *map; - bool ret; - - map = p11_dict_new (p11_dict_str_hash, p11_dict_str_equal, NULL, NULL); - - if (!p11_dict_set (map, key, value)) - assert_not_reached (); - - check = p11_dict_get (map, key); - assert_ptr_eq (check, value); - - ret = p11_dict_remove (map, key); - assert_num_eq (true, ret); - ret = p11_dict_remove (map, key); - assert_num_eq (false, ret); - - check = p11_dict_get (map, key); - assert (check == NULL); - - p11_dict_free (map); -} - -static void -test_set_clear (void) -{ - char *key = "KEY"; - char *value = "VALUE"; - char *check; - p11_dict *map; - - map = p11_dict_new (p11_dict_direct_hash, p11_dict_direct_equal, NULL, NULL); - - if (!p11_dict_set (map, key, value)) - assert_not_reached (); - - p11_dict_clear (map); - - check = p11_dict_get (map, key); - assert (check == NULL); - - p11_dict_free (map); -} - -static void -test_remove_destroys (void) -{ - p11_dict *map; - Key key = { 8, 0 }; - int value = 0; - bool ret; - - map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); - assert_ptr_not_null (map); - if (!p11_dict_set (map, &key, &value)) - assert_not_reached (); - - ret = p11_dict_remove (map, &key); - assert_num_eq (true, ret); - assert_num_eq (true, key.freed); - assert_num_eq (2, value); - - /* should not be destroyed again */ - key.freed = false; - value = 0; - - ret = p11_dict_remove (map, &key); - assert_num_eq (false, ret); - assert_num_eq (false, key.freed); - assert_num_eq (0, value); - - /* should not be destroyed again */ - key.freed = false; - value = 0; - - p11_dict_free (map); - - assert_num_eq (false, key.freed); - assert_num_eq (0, value); -} - -static void -test_set_destroys (void) -{ - p11_dict *map; - Key key = { 8, 0 }; - Key key2 = { 8, 0 }; - int value, value2; - bool ret; - - map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); - assert_ptr_not_null (map); - if (!p11_dict_set (map, &key, &value)) - assert_not_reached (); - - key.freed = key2.freed = false; - value = value2 = 0; - - /* Setting same key and value, should not be destroyed */ - ret = p11_dict_set (map, &key, &value); - assert_num_eq (true, ret); - assert_num_eq (false, key.freed); - assert_num_eq (false, key2.freed); - assert_num_eq (0, value); - assert_num_eq (0, value2); - - key.freed = key2.freed = false; - value = value2 = 0; - - /* Setting a new key same value, key should be destroyed */ - ret = p11_dict_set (map, &key2, &value); - assert_num_eq (true, ret); - assert_num_eq (true, key.freed); - assert_num_eq (false, key2.freed); - assert_num_eq (0, value); - assert_num_eq (0, value2); - - key.freed = key2.freed = false; - value = value2 = 0; - - /* Setting same key, new value, value should be destroyed */ - ret = p11_dict_set (map, &key2, &value2); - assert_num_eq (true, ret); - assert_num_eq (false, key.freed); - assert_num_eq (false, key2.freed); - assert_num_eq (2, value); - assert_num_eq (0, value2); - - key.freed = key2.freed = false; - value = value2 = 0; - - /* Setting new key new value, both should be destroyed */ - ret = p11_dict_set (map, &key, &value); - assert_num_eq (true, ret); - assert_num_eq (false, key.freed); - assert_num_eq (true, key2.freed); - assert_num_eq (0, value); - assert_num_eq (2, value2); - - key.freed = key2.freed = false; - value = value2 = 0; - - p11_dict_free (map); - assert_num_eq (true, key.freed); - assert_num_eq (2, value); - assert_num_eq (false, key2.freed); - assert_num_eq (0, value2); -} - - -static void -test_clear_destroys (void) -{ - p11_dict *map; - Key key = { 18, 0 }; - int value = 0; - - map = p11_dict_new (key_hash, key_equal, key_destroy, value_destroy); - assert_ptr_not_null (map); - if (!p11_dict_set (map, &key, &value)) - assert_not_reached (); - - p11_dict_clear (map); - assert_num_eq (true, key.freed); - assert_num_eq (2, value); - - /* should not be destroyed again */ - key.freed = false; - value = 0; - - p11_dict_clear (map); - assert_num_eq (false, key.freed); - assert_num_eq (0, value); - - /* should not be destroyed again */ - key.freed = false; - value = 0; - - p11_dict_free (map); - - assert_num_eq (false, key.freed); - assert_num_eq (0, value); -} - -static unsigned int -test_hash_intptr_with_collisions (const void *data) -{ - /* lots and lots of collisions, only returns 100 values */ - return (unsigned int)(*((int*)data) % 100); -} - -static void -test_hash_add_check_lots_and_collisions (void) -{ - p11_dict *map; - int *value; - int i; - - map = p11_dict_new (test_hash_intptr_with_collisions, - p11_dict_intptr_equal, NULL, free); - - for (i = 0; i < 20000; ++i) { - value = malloc (sizeof (int)); - assert (value != NULL); - *value = i; - if (!p11_dict_set (map, value, value)) - assert_not_reached (); - } - - for (i = 0; i < 20000; ++i) { - value = p11_dict_get (map, &i); - assert_ptr_not_null (value); - assert_num_eq (i, *value); - } - - p11_dict_free (map); -} - -static void -test_hash_count (void) -{ - p11_dict *map; - int *value; - int i; - bool ret; - - map = p11_dict_new (p11_dict_intptr_hash, p11_dict_intptr_equal, NULL, free); - - assert_num_eq (0, p11_dict_size (map)); - - for (i = 0; i < 20000; ++i) { - value = malloc (sizeof (int)); - assert (value != NULL); - *value = i; - if (!p11_dict_set (map, value, value)) - assert_not_reached (); - assert_num_eq (i + 1, p11_dict_size (map)); - } - - for (i = 0; i < 20000; ++i) { - ret = p11_dict_remove (map, &i); - assert_num_eq (true, ret); - assert_num_eq (20000 - (i + 1), p11_dict_size (map)); - } - - p11_dict_clear (map); - assert_num_eq (0, p11_dict_size (map)); - - p11_dict_free (map); -} - -static void -test_hash_ulongptr (void) -{ - p11_dict *map; - unsigned long *value; - unsigned long i; - - map = p11_dict_new (p11_dict_ulongptr_hash, p11_dict_ulongptr_equal, NULL, free); - - for (i = 0; i < 20000; ++i) { - value = malloc (sizeof (unsigned long)); - assert (value != NULL); - *value = i; - if (!p11_dict_set (map, value, value)) - assert_not_reached (); - } - - for (i = 0; i < 20000; ++i) { - value = p11_dict_get (map, &i); - assert_ptr_not_null (value); - assert_num_eq (i, *value); - } - - p11_dict_free (map); -} - -int -main (int argc, - char *argv[]) -{ - p11_test (test_create, "/dict/create"); - p11_test (test_set_get, "/dict/set-get"); - p11_test (test_set_get_remove, "/dict/set-get-remove"); - p11_test (test_remove_destroys, "/dict/remove-destroys"); - p11_test (test_set_clear, "/dict/set-clear"); - p11_test (test_set_destroys, "/dict/set-destroys"); - p11_test (test_clear_destroys, "/dict/clear-destroys"); - p11_test (test_free_null, "/dict/free-null"); - p11_test (test_free_destroys, "/dict/free-destroys"); - p11_test (test_iterate, "/dict/iterate"); - p11_test (test_iterate_remove, "/dict/iterate-remove"); - p11_test (test_hash_add_check_lots_and_collisions, "/dict/add-check-lots-and-collisions"); - p11_test (test_hash_count, "/dict/count"); - p11_test (test_hash_ulongptr, "/dict/ulongptr"); - return p11_test_run (argc, argv); -} diff --git a/common/test-hash.c b/common/test-hash.c deleted file mode 100644 index a12d5a4..0000000 --- a/common/test-hash.c +++ /dev/null @@ -1,106 +0,0 @@ -/* - * 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 "test.h" - -#include <assert.h> -#include <stdint.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#include "hash.h" - -static void -test_murmur3 (void) -{ - uint32_t one, two, four, seven, eleven, split; - - assert (sizeof (one) == P11_HASH_MURMUR3_LEN); - - p11_hash_murmur3 ((unsigned char *)&one, "one", 3, NULL); - p11_hash_murmur3 ((unsigned char *)&two, "two", 3, NULL); - p11_hash_murmur3 ((unsigned char *)&four, "four", 4, NULL); - p11_hash_murmur3 ((unsigned char *)&seven, "seven", 5, NULL); - p11_hash_murmur3 ((unsigned char *)&eleven, "eleven", 6, NULL); - p11_hash_murmur3 ((unsigned char *)&split, "ele", 3, "ven", 3, NULL); - - assert (one != two); - assert (one != four); - assert (one != seven); - assert (one != eleven); - - assert (two != four); - assert (two != seven); - assert (two != eleven); - - assert (four != seven); - assert (four != eleven); - - assert (split == eleven); -} - -static void -test_murmur3_incr (void) -{ - uint32_t first, second; - - p11_hash_murmur3 ((unsigned char *)&first, - "this is the long input!", (size_t)23, - NULL); - - p11_hash_murmur3 ((unsigned char *)&second, - "this", (size_t)4, - " ", (size_t)1, - "is ", (size_t)3, - "the long ", (size_t)9, - "in", (size_t)2, - "p", (size_t)1, - "u", (size_t)1, - "t", (size_t)1, - "!", (size_t)1, - NULL); - - assert_num_eq (first, second); -} - -int -main (int argc, - char *argv[]) -{ - p11_test (test_murmur3, "/hash/murmur3"); - p11_test (test_murmur3_incr, "/hash/murmur3-incr"); - return p11_test_run (argc, argv); -} diff --git a/common/test-lexer.c b/common/test-lexer.c deleted file mode 100644 index 7d18e87..0000000 --- a/common/test-lexer.c +++ /dev/null @@ -1,253 +0,0 @@ -/* - * Copyright (c) 2013 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@redhat.com> - */ - -#include "config.h" -#include "test.h" - -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#include "compat.h" -#include "debug.h" -#include "lexer.h" -#include "message.h" - -typedef struct { - int tok_type; - const char *name; - const char *value; -} expected_tok; - -static void -check_lex_msg (const char *file, - int line, - const char *function, - const expected_tok *expected, - const char *input, - bool failure) -{ - p11_lexer lexer; - size_t len; - bool failed; - int i; - - p11_lexer_init (&lexer, "test", input, strlen (input)); - for (i = 0; p11_lexer_next (&lexer, &failed); i++) { - if (expected[i].tok_type != lexer.tok_type) - p11_test_fail (file, line, function, - "lexer token type does not match: (%d != %d)", - expected[i].tok_type, lexer.tok_type); - switch (lexer.tok_type) { - case TOK_FIELD: - if (strcmp (expected[i].name, lexer.tok.field.name) != 0) - p11_test_fail (file, line, function, - "field name doesn't match: (%s != %s)", - expected[i].name, lexer.tok.field.name); - if (strcmp (expected[i].value, lexer.tok.field.value) != 0) - p11_test_fail (file, line, function, - "field value doesn't match: (%s != %s)", - expected[i].value, lexer.tok.field.value); - break; - case TOK_SECTION: - if (strcmp (expected[i].name, lexer.tok.field.name) != 0) - p11_test_fail (file, line, function, - "section name doesn't match: (%s != %s)", - expected[i].name, lexer.tok.field.name); - break; - case TOK_PEM: - len = strlen (expected[i].name); - if (lexer.tok.pem.length < len || - strncmp (lexer.tok.pem.begin, expected[i].name, len) != 0) { - p11_test_fail (file, line, function, - "wrong type of PEM block: %s", - expected[i].name); - } - break; - case TOK_EOF: - p11_test_fail (file, line, function, "eof should not be recieved"); - break; - } - } - - if (failure && !failed) - p11_test_fail (file, line, function, "lexing didn't fail"); - else if (!failure && failed) - p11_test_fail (file, line, function, "lexing failed"); - if (TOK_EOF != expected[i].tok_type) - p11_test_fail (file, line, function, "premature end of lexing"); - - p11_lexer_done (&lexer); -} - -#define check_lex_success(expected, input) \ - check_lex_msg (__FILE__, __LINE__, __FUNCTION__, expected, input, false) - -#define check_lex_failure(expected, input) \ - check_lex_msg (__FILE__, __LINE__, __FUNCTION__, expected, input, true) - -static void -test_basic (void) -{ - const char *input = "[the header]\n" - "field: value\n" - "-----BEGIN BLOCK1-----\n" - "aYNNXqshlVxCdo8QfKeXh3GUzd/yn4LYIVgQrx4a\n" - "-----END BLOCK1-----\n"; - - const expected_tok expected[] = { - { TOK_SECTION, "the header" }, - { TOK_FIELD, "field", "value" }, - { TOK_PEM, "-----BEGIN BLOCK1-----\n", }, - { TOK_EOF } - }; - - check_lex_success (expected, input); -} - -static void -test_corners (void) -{ - const char *input = "\r\n" /* blankline */ - " [the header]\r\n" /* bad line endings */ - " field: value \r\n" /* whitespace */ - "number: 2\n" /* extra space*/ - "number :3\n" /* extra space*/ - "number : 4\n" /* extra space*/ - "\n" - " # A comment \n" - "not-a-comment: # value\n" - "-----BEGIN BLOCK1-----\r\n" - "aYNNXqshlVxCdo8QfKeXh3GUzd/yn4LYIVgQrx4a\r\n" - "-----END BLOCK1-----"; /* no new line */ - - const expected_tok expected[] = { - { TOK_SECTION, "the header" }, - { TOK_FIELD, "field", "value" }, - { TOK_FIELD, "number", "2" }, - { TOK_FIELD, "number", "3" }, - { TOK_FIELD, "number", "4" }, - { TOK_FIELD, "not-a-comment", "# value" }, - { TOK_PEM, "-----BEGIN BLOCK1-----\r\n", }, - { TOK_EOF } - }; - - check_lex_success (expected, input); -} - -static void -test_following (void) -{ - const char *input = "-----BEGIN BLOCK1-----\n" - "aYNNXqshlVxCdo8QfKeXh3GUzd/yn4LYIVgQrx4a\n" - "-----END BLOCK1-----\n" - "field: value"; - - const expected_tok expected[] = { - { TOK_PEM, "-----BEGIN BLOCK1-----\n", }, - { TOK_FIELD, "field", "value" }, - { TOK_EOF } - }; - - check_lex_success (expected, input); -} - -static void -test_bad_pem (void) -{ - const char *input = "field: value\n" - "-----BEGIN BLOCK1-----\n" - "aYNNXqshlVxCdo8QfKeXh3GUzd/yn4LYIVgQrx4a\n"; - - const expected_tok expected[] = { - { TOK_FIELD, "field", "value" }, - { TOK_EOF } - }; - - p11_message_quiet (); - - check_lex_failure (expected, input); - - p11_message_loud (); -} - -static void -test_bad_section (void) -{ - const char *input = "field: value\n" - "[section\n" - "bad]\n"; - - const expected_tok expected[] = { - { TOK_FIELD, "field", "value" }, - { TOK_EOF } - }; - - p11_message_quiet (); - - check_lex_failure (expected, input); - - p11_message_loud (); -} - -static void -test_bad_value (void) -{ - const char *input = "field_value\n" - "[section\n" - "bad]\n"; - - const expected_tok expected[] = { - { TOK_EOF } - }; - - p11_message_quiet (); - - check_lex_failure (expected, input); - - p11_message_loud (); -} - -int -main (int argc, - char *argv[]) -{ - p11_test (test_basic, "/lexer/basic"); - p11_test (test_corners, "/lexer/corners"); - p11_test (test_following, "/lexer/following"); - p11_test (test_bad_pem, "/lexer/bad-pem"); - p11_test (test_bad_section, "/lexer/bad-section"); - p11_test (test_bad_value, "/lexer/bad-value"); - return p11_test_run (argc, argv); -} diff --git a/common/test-message.c b/common/test-message.c deleted file mode 100644 index 63ecf31..0000000 --- a/common/test-message.c +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2013 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@redhat.com> - */ - -#include "config.h" -#include "test.h" - -#include "message.h" - -#include <errno.h> -#include <stdlib.h> - -static void -test_with_err (void) -{ - const char *last; - char *expected; - - errno = E2BIG; - p11_message_err (ENOENT, "Details: %s", "value"); - last = p11_message_last (); - - if (asprintf (&expected, "Details: value: %s", strerror (ENOENT)) < 0) - assert_not_reached (); - assert_str_eq (expected, last); - free (expected); -} - -int -main (int argc, - char *argv[]) -{ - p11_test (test_with_err, "/message/with-err"); - return p11_test_run (argc, argv); -} diff --git a/common/test-path.c b/common/test-path.c deleted file mode 100644 index 57619c8..0000000 --- a/common/test-path.c +++ /dev/null @@ -1,216 +0,0 @@ -/* - * Copyright (c) 2013 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@redhat.com> - */ - -#include "config.h" -#include "test.h" - -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#include "compat.h" -#include "path.h" - -static void -test_base (void) -{ - struct { - const char *in; - const char *out; - } fixtures[] = { - { "/this/is/a/path", "path" }, - { "/this/is/a/folder/", "folder" }, - { "folder/", "folder" }, - { "/", "" }, - { "this", "this" }, -#ifdef OS_WIN32 - { "\\this\\is\\a\\path", "path" }, - { "\\this\\is\\a\\folder\\", "folder" }, - { "C:\\this\\is\\a\\path", "path" }, - { "D:\\this\\is\\a\\folder\\", "folder" }, - { "folder\\", "folder" }, - { "\\", "" }, -#endif - { NULL }, - }; - - char *out; - int i; - - for (i = 0; fixtures[i].in != NULL; i++) { - out = p11_path_base (fixtures[i].in); - assert_str_eq (fixtures[i].out, out); - free (out); - } -} - -#define assert_str_eq_free(ex, ac) \ - do { const char *__s1 = (ex); \ - char *__s2 = (ac); \ - if (__s1 && __s2 && strcmp (__s1, __s2) == 0) ; else \ - p11_test_fail (__FILE__, __LINE__, __FUNCTION__, "assertion failed (%s == %s): (%s == %s)", \ - #ex, #ac, __s1 ? __s1 : "(null)", __s2 ? __s2 : "(null)"); \ - free (__s2); \ - } while (0) - -static void -test_build (void) -{ -#ifdef OS_UNIX - assert_str_eq_free ("/root/second", - p11_path_build ("/root", "second", NULL)); - assert_str_eq_free ("/root/second", - p11_path_build ("/root", "/second", NULL)); - assert_str_eq_free ("/root/second", - p11_path_build ("/root/", "second", NULL)); - assert_str_eq_free ("/root/second/third", - p11_path_build ("/root", "second", "third", NULL)); - assert_str_eq_free ("/root/second/third", - p11_path_build ("/root", "/second/third", NULL)); -#else /* OS_WIN32 */ - assert_str_eq_free ("C:\\root\\second", - p11_path_build ("C:\\root", "second", NULL)); - assert_str_eq_free ("C:\\root\\second", - p11_path_build ("C:\\root", "\\second", NULL)); - assert_str_eq_free ("C:\\root\\second", - p11_path_build ("C:\\root\\", "second", NULL)); - assert_str_eq_free ("C:\\root\\second\\third", - p11_path_build ("C:\\root", "second", "third", NULL)); - assert_str_eq_free ("C:\\root\\second/third", - p11_path_build ("C:\\root", "second/third", NULL)); -#endif -} - -static void -test_expand (void) -{ - char *path; - -#ifdef OS_UNIX - putenv ("HOME=/home/blah"); - assert_str_eq_free ("/home/blah/my/path", - p11_path_expand ("~/my/path")); - assert_str_eq_free ("/home/blah", - p11_path_expand ("~")); - putenv ("XDG_CONFIG_HOME=/my"); - assert_str_eq_free ("/my/path", - p11_path_expand ("~/.config/path")); - putenv ("XDG_CONFIG_HOME="); - assert_str_eq_free ("/home/blah/.config/path", - p11_path_expand ("~/.config/path")); -#else /* OS_WIN32 */ - putenv ("HOME=C:\\Users\\blah"); - assert_str_eq_free ("C:\\Users\\blah\\path", - p11_path_expand ("~/my/path")); - assert_str_eq_free ("C:\\Users\\blah\\path", - p11_path_expand ("~\\path")); -#endif - - putenv("HOME="); - path = p11_path_expand ("~/this/is/my/path"); - assert (strstr (path, "this/is/my/path") != NULL); - free (path); -} - -static void -test_absolute (void) -{ -#ifdef OS_UNIX - assert (p11_path_absolute ("/home")); - assert (!p11_path_absolute ("home")); -#else /* OS_WIN32 */ - assert (p11_path_absolute ("C:\\home")); - assert (!p11_path_absolute ("home")); - assert (p11_path_absolute ("/home")); -#endif -} - -static void -test_parent (void) -{ - assert_str_eq_free ("/", p11_path_parent ("/root")); - assert_str_eq_free ("/", p11_path_parent ("/root/")); - assert_str_eq_free ("/", p11_path_parent ("/root//")); - assert_str_eq_free ("/root", p11_path_parent ("/root/second")); - assert_str_eq_free ("/root", p11_path_parent ("/root//second")); - assert_str_eq_free ("/root", p11_path_parent ("/root//second//")); - assert_str_eq_free ("/root", p11_path_parent ("/root///second")); - assert_str_eq_free ("/root/second", p11_path_parent ("/root/second/test.file")); - assert_ptr_eq (NULL, p11_path_parent ("/")); - assert_ptr_eq (NULL, p11_path_parent ("//")); - assert_ptr_eq (NULL, p11_path_parent ("")); -} - -static void -test_prefix (void) -{ - assert (p11_path_prefix ("/test/second", "/test")); - assert (!p11_path_prefix ("/test", "/test")); - assert (!p11_path_prefix ("/different/prefix", "/test")); - assert (!p11_path_prefix ("/te", "/test")); - assert (!p11_path_prefix ("/test", "/test/blah")); - assert (p11_path_prefix ("/test/other/second", "/test")); - assert (p11_path_prefix ("/test//other//second", "/test")); -} - -static void -test_canon (void) -{ - char *test; - - test = strdup ("2309haonutb;AOE@#$O "); - p11_path_canon (test); - assert_str_eq (test, "2309haonutb_AOE___O_"); - free (test); - - test = strdup ("22@# %ATI@#$onot"); - p11_path_canon (test); - assert_str_eq (test, "22____ATI___onot"); - free (test); -} - -int -main (int argc, - char *argv[]) -{ - p11_test (test_base, "/path/base"); - p11_test (test_build, "/path/build"); - p11_test (test_expand, "/path/expand"); - p11_test (test_absolute, "/path/absolute"); - p11_test (test_parent, "/path/parent"); - p11_test (test_prefix, "/path/prefix"); - p11_test (test_canon, "/path/canon"); - - return p11_test_run (argc, argv); -} diff --git a/common/test-tests.c b/common/test-tests.c deleted file mode 100644 index ba31d83..0000000 --- a/common/test-tests.c +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright (c) 2013 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@redhat.com> - */ - -#include "config.h" -#include "test.h" - -#include <stdlib.h> - -static void -test_success (void) -{ - /* Yup, nothing */ -} - - -static void -test_failure (void) -{ - if (getenv ("TEST_FAIL")) { - p11_test_fail (__FILE__, __LINE__, __FUNCTION__, - "Unconditional test failure due to TEST_FAIL environment variable"); - } -} - -static void -test_memory (void) -{ - char *mem; - - if (getenv ("TEST_FAIL")) { - mem = malloc (1); - assert (mem != NULL); - free (mem); - *mem = 1; - } -} - - -static void -test_leak (void) -{ - char *mem; - - if (getenv ("TEST_FAIL")) { - mem = malloc (1); - assert (mem != NULL); - *mem = 1; - } -} - -int -main (int argc, - char *argv[]) -{ - p11_test (test_success, "/test/success"); - - if (getenv ("TEST_FAIL")) { - p11_test (test_failure, "/test/failure"); - p11_test (test_memory, "/test/memory"); - p11_test (test_leak, "/test/leak"); - } - - return p11_test_run (argc, argv); -} diff --git a/common/test-url.c b/common/test-url.c deleted file mode 100644 index 892bf3c..0000000 --- a/common/test-url.c +++ /dev/null @@ -1,164 +0,0 @@ -/* - * Copyright (c) 2013 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@redhat.com> - */ - -#include "config.h" -#include "test.h" - -#include "debug.h" -#include "message.h" - -#include <assert.h> -#include <string.h> -#include <stdio.h> -#include <stdlib.h> - -#include "url.h" - -static void -check_decode_msg (const char *file, - int line, - const char *function, - const char *input, - ssize_t input_len, - const char *expected, - size_t expected_len) -{ - unsigned char *decoded; - size_t length; - - if (input_len < 0) - input_len = strlen (input); - decoded = p11_url_decode (input, input + input_len, "", &length); - - if (expected == NULL) { - if (decoded != NULL) - p11_test_fail (file, line, function, "decoding should have failed"); - - } else { - if (decoded == NULL) - p11_test_fail (file, line, function, "decoding failed"); - if (expected_len != length) - p11_test_fail (file, line, function, "wrong length: (%lu != %lu)", - (unsigned long)expected_len, (unsigned long)length); - if (memcmp (decoded, expected, length) != 0) - p11_test_fail (file, line, function, "decoding wrong"); - free (decoded); - } -} - -#define check_decode_success(input, input_len, expected, expected_len) \ - check_decode_msg (__FILE__, __LINE__, __FUNCTION__, input, input_len, expected, expected_len) - -#define check_decode_failure(input, input_len) \ - check_decode_msg (__FILE__, __LINE__, __FUNCTION__, input, input_len, NULL, 0) - -static void -test_decode_success (void) -{ - check_decode_success ("%54%45%53%54%00", -1, "TEST", 5); - check_decode_success ("%54%45%53%54%00", 6, "TE", 2); - check_decode_success ("%54est%00", -1, "Test", 5); -} - -static void -test_decode_skip (void) -{ - const char *input = "%54 %45 %53 %54 %00"; - unsigned char *decoded; - size_t length; - - decoded = p11_url_decode (input, input + strlen (input), P11_URL_WHITESPACE, &length); - assert_str_eq ("TEST", (char *)decoded); - assert_num_eq (5, length); - - free (decoded); -} - -static void -test_decode_failure (void) -{ - /* Early termination */ - check_decode_failure ("%54%45%53%5", -1); - check_decode_failure ("%54%45%53%", -1); - - /* Not hex characters */ - check_decode_failure ("%54%XX%53%54%00", -1); -} - -static void -test_encode (void) -{ - const unsigned char *input = (unsigned char *)"TEST"; - p11_buffer buf; - - if (!p11_buffer_init_null (&buf, 5)) - assert_not_reached (); - - p11_url_encode (input, input + 5, "", &buf); - assert (p11_buffer_ok (&buf)); - assert_str_eq ("%54%45%53%54%00", (char *)buf.data); - assert_num_eq (15, buf.len); - - p11_buffer_uninit (&buf); -} - -static void -test_encode_verbatim (void) -{ - const unsigned char *input = (unsigned char *)"TEST"; - p11_buffer buf; - - if (!p11_buffer_init_null (&buf, 5)) - assert_not_reached (); - - p11_url_encode (input, input + 5, "ES", &buf); - assert (p11_buffer_ok (&buf)); - assert_str_eq ("%54ES%54%00", (char *)buf.data); - assert_num_eq (11, buf.len); - - p11_buffer_uninit (&buf); -} - -int -main (int argc, - char *argv[]) -{ - p11_test (test_decode_success, "/url/decode-success"); - p11_test (test_decode_skip, "/url/decode-skip"); - p11_test (test_decode_failure, "/url/decode-failure"); - - p11_test (test_encode, "/url/encode"); - p11_test (test_encode_verbatim, "/url/encode-verbatim"); - return p11_test_run (argc, argv); -} diff --git a/common/test.c b/common/test.c deleted file mode 100644 index 9605d03..0000000 --- a/common/test.c +++ /dev/null @@ -1,548 +0,0 @@ -/* - * Copyright (c) 2013, 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@redhat.com> - */ - -#include "config.h" - -#define P11_TEST_SOURCE 1 - -#include "compat.h" -#include "test.h" -#include "debug.h" -#include "path.h" - -#include <assert.h> -#include <dirent.h> -#include <errno.h> -#include <setjmp.h> -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> - -#ifdef OS_UNIX -#include <sys/stat.h> -#include <sys/wait.h> -#endif - -enum { - FIXTURE, - TEST, -}; - -typedef void (*func_with_arg) (void *); - -typedef struct _test_item { - int type; - - union { - struct { - char name[1024]; - func_with_arg func; - void *argument; - int failed; - } test; - struct { - func_with_arg setup; - func_with_arg teardown; - } fix; - } x; - - struct _test_item *next; -} test_item; - -struct { - test_item *suite; - test_item *last; - int number; - jmp_buf jump; -} gl = { NULL, NULL, 0, }; - -void -p11_test_fail (const char *filename, - int line, - const char *function, - const char *message, - ...) -{ - const char *pos; - char *output; - char *from; - char *next; - va_list va; - - assert (gl.last != NULL); - assert (gl.last->type == TEST); - gl.last->x.test.failed = 1; - - printf ("not ok %d %s\n", gl.number, gl.last->x.test.name); - - va_start (va, message); - if (vasprintf (&output, message, va) < 0) - assert (0 && "vasprintf() failed"); - va_end (va); - - for (from = output; from != NULL; ) { - next = strchr (from, '\n'); - if (next) { - next[0] = '\0'; - next += 1; - } - - printf ("# %s\n", from); - from = next; - } - - pos = strrchr (filename, '/'); - if (pos != NULL && pos[1] != '\0') - filename = pos + 1; - - printf ("# in %s() at %s:%d\n", function, filename, line); - - free (output); - - /* Let coverity know we're not supposed to return from here */ -#ifdef __COVERITY__ - abort(); -#endif - - longjmp (gl.jump, 1); -} - -static void -test_push (test_item *it) -{ - test_item *item; - - item = calloc (1, sizeof (test_item)); - assert (item != NULL); - memcpy (item, it, sizeof (test_item)); - - if (!gl.suite) - gl.suite = item; - if (gl.last) - gl.last->next = item; - gl.last = item; -} - -void -p11_test (void (* function) (void), - const char *name, - ...) -{ - test_item item = { TEST, }; - va_list va; - - item.x.test.func = (func_with_arg)function; - - va_start (va, name); - vsnprintf (item.x.test.name, sizeof (item.x.test.name), name, va); - va_end (va); - - test_push (&item); -} - -void -p11_testx (void (* function) (void *), - void *argument, - const char *name, - ...) -{ - test_item item = { TEST, }; - va_list va; - - item.type = TEST; - item.x.test.func = function; - item.x.test.argument = argument; - - va_start (va, name); - vsnprintf (item.x.test.name, sizeof (item.x.test.name), name, va); - va_end (va); - - test_push (&item); -} - -void -p11_fixture (void (* setup) (void *), - void (* teardown) (void *)) -{ - test_item item; - - item.type = FIXTURE; - item.x.fix.setup = setup; - item.x.fix.teardown = teardown; - - test_push (&item); -} - -static int -should_run_test (int argc, - char **argv, - test_item *item) -{ - int i; - if (argc == 0) - return 1; - for (i = 0; i < argc; i++) { - if (strcmp (argv[i], item->x.test.name) == 0) - return 1; - } - - return 0; -} - -int -p11_test_run (int argc, - char **argv) -{ - test_item *fixture = NULL; - test_item *item; - test_item *next; - int count; - int ret = 0; - int setup; - int opt; - - /* p11-kit specific stuff */ - putenv ("P11_KIT_STRICT=1"); - p11_debug_init (); - - while ((opt = getopt (argc, argv, "")) != -1) { - switch (opt) { - default: - fprintf (stderr, "specify only test names on the command line\n"); - return 2; - } - } - - argc -= optind; - argv += optind; - - assert (gl.number == 0); - gl.last = NULL; - - for (item = gl.suite, count = 0; item != NULL; item = item->next) { - if (item->type == TEST && should_run_test (argc, argv, item)) - count++; - } - - if (count == 0) { - printf ("1..0 # No tests\n"); - return 0; - } - - printf ("1..%d\n", count); - - for (item = gl.suite, gl.number = 0; item != NULL; item = item->next) { - if (item->type == FIXTURE) { - fixture = item; - continue; - } - - assert (item->type == TEST); - - if (!should_run_test (argc, argv, item)) - continue; - - gl.last = item; - gl.number++; - setup = 0; - - if (setjmp (gl.jump) == 0) { - if (fixture && fixture->x.fix.setup) - (fixture->x.fix.setup) (item->x.test.argument); - - setup = 1; - - assert (item->x.test.func); - (item->x.test.func)(item->x.test.argument); - - printf ("ok %d %s\n", gl.number, item->x.test.name); - } - - if (setup) { - if (setjmp (gl.jump) == 0) { - if (fixture && fixture->x.fix.teardown) - (fixture->x.fix.teardown) (item->x.test.argument); - } - } - - gl.last = NULL; - } - - for (item = gl.suite; item != NULL; item = next) { - if (item->type == TEST) { - if (item->x.test.failed) - ret++; - } - - next = item->next; - free (item); - } - - gl.suite = NULL; - gl.last = 0; - gl.number = 0; - return ret; -} - -static char * -expand_tempdir (const char *name) -{ - const char *env; - - env = secure_getenv ("TMPDIR"); - if (env && env[0]) { - return p11_path_build (env, name, NULL); - - } else { -#ifdef OS_UNIX -#ifdef _PATH_TMP - return p11_path_build (_PATH_TMP, name, NULL); -#else - return p11_path_build ("/tmp", name, NULL); -#endif - -#else /* OS_WIN32 */ - char directory[MAX_PATH + 1]; - - if (!GetTempPathA (MAX_PATH + 1, directory)) { - printf ("# couldn't lookup temp directory\n"); - errno = ENOTDIR; - return NULL; - } - - return p11_path_build (directory, name, NULL); - -#endif /* OS_WIN32 */ - } -} - -char * -p11_test_directory (const char *prefix) -{ - char *templ; - char *directory; - - if (asprintf (&templ, "%s.XXXXXX", prefix) < 0) - assert_not_reached (); - - directory = expand_tempdir (templ); - assert (directory != NULL); - - if (!mkdtemp (directory)) { - printf ("# couldn't create temp directory: %s: %s\n", - directory, strerror (errno)); - free (directory); - assert_not_reached (); - } - - free (templ); - return directory; -} - -void -p11_test_file_write (const char *base, - const char *name, - const void *contents, - size_t length) -{ - char *path = NULL; - FILE *f; - - if (base) { - if (asprintf (&path, "%s/%s", base, name) < 0) - assert_not_reached (); - name = path; - } - - f = fopen (name, "wb"); - if (f == NULL) { - printf ("# couldn't open file for writing: %s: %s\n", name, strerror (errno)); - free (path); - assert_not_reached (); - } - - if (fwrite (contents, 1, length, f) != length || - fclose (f) != 0) { - printf ("# couldn't write to file: %s: %s\n", name, strerror (errno)); - free (path); - assert_not_reached (); - } - - free (path); -} - -void -p11_test_file_delete (const char *base, - const char *name) -{ - char *path = NULL; - - if (base) { - if (asprintf (&path, "%s/%s", base, name) < 0) - assert_not_reached (); - name = path; - } - - if (unlink (name) < 0) { - printf ("# Couldn't delete file: %s\n", name); - free (path); - assert_not_reached (); - } - - free (path); -} - -void -p11_test_directory_delete (const char *directory) -{ - struct dirent *dp; - DIR *dir; - - dir = opendir (directory); - if (dir == NULL) { - printf ("# Couldn't open directory: %s\n", directory); - assert_not_reached (); - } - - while ((dp = readdir (dir)) != NULL) { - if (strcmp (dp->d_name, ".") == 0 || - strcmp (dp->d_name, "..") == 0) - continue; - - p11_test_file_delete (directory, dp->d_name); - } - - closedir (dir); - - if (rmdir (directory) < 0) { - printf ("# Couldn't remove directory: %s\n", directory); - assert_not_reached (); - } -} - - -#ifdef OS_UNIX - -static void -copy_file (const char *input, - int fd) -{ - p11_mmap *mmap; - const char *data; - ssize_t written; - size_t size; - - mmap = p11_mmap_open (input, NULL, (void **)&data, &size); - assert (mmap != NULL); - - while (size > 0) { - written = write (fd, data, size); - assert (written >= 0); - - data += written; - size -= written; - } - - p11_mmap_close (mmap); -} - -char * -p11_test_copy_setgid (const char *input) -{ - gid_t groups[128]; - char *path; - gid_t group = 0; - int ret; - int fd; - int i; - - ret = getgroups (128, groups); - for (i = 0; i < ret; ++i) { - if (groups[i] != getgid ()) { - group = groups[i]; - break; - } - } - if (i == ret) { - fprintf (stderr, "# no suitable group, skipping test\n"); - return NULL; - } - - path = strdup ("/tmp/test-setgid.XXXXXX"); - assert (path != NULL); - - fd = mkstemp (path); - assert (fd >= 0); - - copy_file (input, fd); - if (fchown (fd, getuid (), group) < 0) - assert_not_reached (); - if (fchmod (fd, 02750) < 0) - assert_not_reached (); - if (close (fd) < 0) - assert_not_reached (); - - return path; -} - -int -p11_test_run_child (const char **argv, - bool quiet_out) -{ - pid_t child; - int status; - - child = fork (); - assert (child >= 0); - - /* In the child process? */ - if (child == 0) { - if (quiet_out) - close (1); /* stdout */ - execv (argv[0], (char **)argv); - assert_not_reached (); - } - - if (waitpid (child, &status, 0) < 0) - assert_not_reached (); - - assert (!WIFSIGNALED (status)); - assert (WIFEXITED (status)); - - return WEXITSTATUS (status); -} - -#endif /* OS_UNIX */ diff --git a/common/test.h b/common/test.h deleted file mode 100644 index e28bb55..0000000 --- a/common/test.h +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Copyright (c) 2013, 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@redhat.com> - */ - -#include "compat.h" - -#ifndef P11_TEST_H_ -#define P11_TEST_H_ - -#ifndef P11_TEST_SOURCE - -#include <string.h> - -#ifdef assert_not_reached -#undef assert_not_reached -#endif - -#ifdef assert -#undef assert -#endif - -#define assert(expr) \ - assert_true(expr) -#define assert_true(expr) \ - do { if (expr) ; else \ - p11_test_fail (__FILE__, __LINE__, __FUNCTION__, "assertion failed (%s)", #expr); \ - } while (0) -#define assert_false(expr) \ - do { if (expr) \ - p11_test_fail (__FILE__, __LINE__, __FUNCTION__, "assertion failed (!(%s))", #expr); \ - } while (0) -#define assert_fail(msg, detail) \ - do { const char *__s = (detail); \ - p11_test_fail (__FILE__, __LINE__, __FUNCTION__, "%s%s%s", (msg), __s ? ": ": "", __s ? __s : ""); \ - } while (0) -#define assert_not_reached(msg) \ - do { \ - p11_test_fail (__FILE__, __LINE__, __FUNCTION__, "code should not be reached"); \ - } while (0) -#define assert_ptr_not_null(ptr) \ - do { if ((ptr) != NULL) ; else \ - p11_test_fail (__FILE__, __LINE__, __FUNCTION__, "assertion failed (%s != NULL)", #ptr); \ - } while (0) -#define assert_num_cmp(a1, cmp, a2) \ - do { unsigned long __n1 = (a1); \ - unsigned long __n2 = (a2); \ - if (__n1 cmp __n2) ; else \ - p11_test_fail (__FILE__, __LINE__, __FUNCTION__, "assertion failed (%s %s %s): (%lu %s %lu)", \ - #a1, #cmp, #a2, __n1, #cmp, __n2); \ - } while (0) -#define assert_num_eq(a1, a2) \ - assert_num_cmp(a1, ==, a2) -#define assert_str_cmp(a1, cmp, a2) \ - do { const char *__s1 = (a1); \ - const char *__s2 = (a2); \ - if (__s1 && __s2 && strcmp (__s1, __s2) cmp 0) ; else \ - p11_test_fail (__FILE__, __LINE__, __FUNCTION__, "assertion failed (%s %s %s): (%s %s %s)", \ - #a1, #cmp, #a2, __s1 ? __s1 : "(null)", #cmp, __s2 ? __s2 : "(null)"); \ - } while (0) -#define assert_str_eq(a1, a2) \ - assert_str_cmp(a1, ==, a2) -#define assert_ptr_eq(a1, a2) \ - do { const void *__p1 = (a1); \ - const void *__p2 = (a2); \ - if (__p1 == __p2) ; else \ - p11_test_fail (__FILE__, __LINE__, __FUNCTION__, "assertion failed (%s == %s): (0x%08lx == 0x%08lx)", \ - #a1, #a2, (unsigned long)(size_t)__p1, (unsigned long)(size_t)__p2); \ - } while (0) - -#define assert_str_contains(expr, needle) \ - do { const char *__str = (expr); \ - if (__str && strstr (__str, needle)) ; else \ - p1_test_fail (__FILE__, __LINE__, __FUNCTION__, "assertion failed (%s): '%s' does not contain '%s'", \ - #expr, __str, needle); \ - } while (0) - -#endif /* !P11_TEST_SOURCE */ - - -void p11_test_fail (const char *filename, - int line, - const char *function, - const char *message, - ...) GNUC_PRINTF(4, 5) CLANG_ANALYZER_NORETURN; - -void p11_test (void (* function) (void), - const char *name, - ...) GNUC_PRINTF(2, 3); - -void p11_testx (void (* function) (void *), - void *argument, - const char *name, - ...) GNUC_PRINTF(3, 4); - -void p11_fixture (void (* setup) (void *), - void (* teardown) (void *)); - -int p11_test_run (int argc, - char **argv); - -char * p11_test_directory (const char *prefix); - -void p11_test_directory_delete (const char *directory); - -void p11_test_file_write (const char *directory, - const char *name, - const void *contents, - size_t length); - -void p11_test_file_delete (const char *directory, - const char *name); - -#ifdef OS_UNIX - -char * p11_test_copy_setgid (const char *path); - -int p11_test_run_child (const char **argv, - bool quiet_out); - -#endif - -#endif /* P11_TEST_H_ */ diff --git a/common/tool.c b/common/tool.c deleted file mode 100644 index cca18a2..0000000 --- a/common/tool.c +++ /dev/null @@ -1,333 +0,0 @@ -/* - * 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> - */ - -#include "config.h" - -#include "buffer.h" -#include "compat.h" -#include "debug.h" -#include "message.h" -#include "path.h" - -#include <assert.h> -#include <ctype.h> -#include <getopt.h> -#include <string.h> -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> - -#include "tool.h" - -static char -short_option (int opt) -{ - if (isalpha (opt) || isdigit (opt)) - return (char)opt; - return 0; -} - -static const struct option * -find_option (const struct option *longopts, - int opt) -{ - int i; - - for (i = 0; longopts[i].name != NULL; i++) { - if (longopts[i].val == opt) - return longopts + i; - } - - return NULL; -} - -void -p11_tool_usage (const p11_tool_desc *usages, - const struct option *longopts) -{ - const struct option *longopt; - const int indent = 22; - const char *long_name; - const char *description; - const char *next; - char short_name; - int spaces; - int len; - int i; - - for (i = 0; usages[i].text != NULL; i++) { - - /* If no option, then this is a heading */ - if (!usages[i].option) { - printf ("%s\n\n", usages[i].text); - continue; - } - - longopt = find_option (longopts, usages[i].option); - long_name = longopt ? longopt->name : NULL; - short_name = short_option (usages[i].option); - description = usages[i].text; - - if (short_name && long_name) - len = printf (" -%c, --%s", (int)short_name, long_name); - else if (long_name) - len = printf (" --%s", long_name); - else - len = printf (" -%c", (int)short_name); - if (longopt && longopt->has_arg) - len += printf ("%s<%s>", - long_name ? "=" : " ", - usages[i].arg ? usages[i].arg : "..."); - if (len < indent) { - spaces = indent - len; - } else { - printf ("\n"); - spaces = indent; - } - while (description) { - while (spaces-- > 0) - fputc (' ', stdout); - next = strchr (description, '\n'); - if (next) { - next += 1; - printf ("%.*s", (int)(next - description), description); - description = next; - spaces = indent; - } else { - printf ("%s\n", description); - break; - } - } - - } -} - -int -p11_tool_getopt (int argc, - char *argv[], - const struct option *longopts) -{ - p11_buffer buf; - int ret; - char opt; - int i; - - if (!p11_buffer_init_null (&buf, 64)) - return_val_if_reached (-1); - - for (i = 0; longopts[i].name != NULL; i++) { - opt = short_option (longopts[i].val); - if (opt != 0) { - p11_buffer_add (&buf, &opt, 1); - assert (longopts[i].has_arg != optional_argument); - if (longopts[i].has_arg == required_argument) - p11_buffer_add (&buf, ":", 1); - } - } - - ret = getopt_long (argc, argv, buf.data, longopts, NULL); - - p11_buffer_uninit (&buf); - - return ret; -} - -static void -command_usage (const p11_tool_command *commands) -{ - const char *progname; - int i; - - progname = getprogname (); - printf ("usage: %s command <args>...\n", progname); - printf ("\nCommon %s commands are:\n", progname); - for (i = 0; commands[i].name != NULL; i++) { - if (strcmp (commands[i].name, P11_TOOL_FALLBACK) != 0) - printf (" %-15s %s\n", commands[i].name, commands[i].text); - } - printf ("\nSee '%s <command> --help' for more information\n", progname); -} - -static void -verbose_arg (void) -{ - putenv ("P11_KIT_DEBUG=tool"); - p11_message_loud (); - p11_debug_init (); -} - -static void -quiet_arg (void) -{ - putenv ("P11_KIT_DEBUG="); - p11_message_quiet (); - p11_debug_init (); -} - -int -p11_tool_main (int argc, - char *argv[], - const p11_tool_command *commands) -{ - const p11_tool_command *fallback = NULL; - char *command = NULL; - bool want_help = false; - bool skip; - int in, out; - int i; - - /* - * Parse the global options. We rearrange the options as - * necessary, in order to pass relevant options through - * to the commands, but also have them take effect globally. - */ - - for (in = 1, out = 1; in < argc; in++, out++) { - - /* The non-option is the command, take it out of the arguments */ - if (argv[in][0] != '-') { - if (!command) { - skip = true; - command = argv[in]; - } else { - skip = false; - } - - /* The global long options */ - } else if (argv[in][1] == '-') { - skip = false; - - if (strcmp (argv[in], "--") == 0) { - if (!command) { - p11_message ("no command specified"); - return 2; - } else { - break; - } - - } else if (strcmp (argv[in], "--verbose") == 0) { - verbose_arg (); - - } else if (strcmp (argv[in], "--quiet") == 0) { - quiet_arg (); - - } else if (strcmp (argv[in], "--help") == 0) { - want_help = true; - - } else if (!command) { - p11_message ("unknown global option: %s", argv[in]); - return 2; - } - - /* The global short options */ - } else { - skip = false; - - for (i = 1; argv[in][i] != '\0'; i++) { - switch (argv[in][i]) { - case 'h': - want_help = true; - break; - - /* Compatibility option */ - case 'l': - command = "list-modules"; - break; - - case 'v': - verbose_arg (); - break; - - case 'q': - quiet_arg (); - break; - - default: - if (!command) { - p11_message ("unknown global option: -%c", (int)argv[in][i]); - return 2; - } - break; - } - } - } - - /* Skipping this argument? */ - if (skip) - out--; - else - argv[out] = argv[in]; - } - - /* Initialize tool's debugging after setting env vars above */ - p11_debug_init (); - - if (command == NULL) { - /* As a special favor if someone just typed the command, help them out */ - if (argc == 1) { - command_usage (commands); - return 2; - } else if (want_help) { - command_usage (commands); - return 0; - } else { - p11_message ("no command specified"); - return 2; - } - } - - argc = out; - - /* Look for the command */ - for (i = 0; commands[i].name != NULL; i++) { - if (strcmp (commands[i].name, P11_TOOL_FALLBACK) == 0) { - fallback = commands + i; - - } else if (strcmp (commands[i].name, command) == 0) { - argv[0] = command; - return (commands[i].function) (argc, argv); - } - } - - /* Got here because no command matched */ - if (fallback != NULL) { - argv[0] = command; - return (fallback->function) (argc, argv); - } - - /* At this point we have no command */ - p11_message ("'%s' is not a valid command. See '%s --help'", - command, getprogname ()); - return 2; -} diff --git a/common/tool.h b/common/tool.h deleted file mode 100644 index 16785da..0000000 --- a/common/tool.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2013 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@collabora.co.uk> - */ - -#ifndef P11_TOOL_H_ -#define P11_TOOL_H_ - -#include <getopt.h> - -#define P11_TOOL_FALLBACK "" - -typedef struct { - const char *name; - int (*function) (int, char*[]); - const char *text; -} p11_tool_command; - -typedef struct { - int option; - const char *text; - const char *arg; -} p11_tool_desc; - -int p11_tool_main (int argc, - char *argv[], - const p11_tool_command *commands); - -int p11_tool_getopt (int argc, - char *argv[], - const struct option *longopts); - -void p11_tool_usage (const p11_tool_desc *usages, - const struct option *longopts); - -#endif /* P11_TOOL_H_ */ diff --git a/common/url.c b/common/url.c deleted file mode 100644 index 4b7e47b..0000000 --- a/common/url.c +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright (C) 2011 Collabora Ltd. - * Copyright (C) 2013 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@collabora.co.uk> - */ - -#include "config.h" - -#include "debug.h" -#include "url.h" - -#include <assert.h> -#include <ctype.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -const static char HEX_CHARS[] = "0123456789abcdef"; - -unsigned char * -p11_url_decode (const char *value, - const char *end, - const char *skip, - size_t *length) -{ - char *a, *b; - unsigned char *result, *p; - - assert (value <= end); - assert (skip != NULL); - - /* String can only get shorter */ - result = malloc ((end - value) + 1); - return_val_if_fail (result != NULL, NULL); - - /* Now loop through looking for escapes */ - p = result; - while (value != end) { - /* - * A percent sign followed by two hex digits means - * that the digits represent an escaped character. - */ - if (*value == '%') { - value++; - if (value + 2 > end) { - free (result); - return NULL; - } - a = strchr (HEX_CHARS, tolower (value[0])); - b = strchr (HEX_CHARS, tolower (value[1])); - if (!a || !b) { - free (result); - return NULL; - } - *p = (a - HEX_CHARS) << 4; - *(p++) |= (b - HEX_CHARS); - value += 2; - - /* Ignore whitespace characters */ - } else if (strchr (skip, *value)) { - value++; - - /* A different character */ - } else { - *(p++) = *(value++); - } - } - - /* Null terminate string, in case its a string */ - *p = 0; - - if (length) - *length = p - result; - return result; -} - -void -p11_url_encode (const unsigned char *value, - const unsigned char *end, - const char *verbatim, - p11_buffer *buf) -{ - char hex[3]; - - assert (value <= end); - - /* Now loop through looking for escapes */ - while (value != end) { - - /* These characters we let through verbatim */ - if (*value && strchr (verbatim, *value) != NULL) { - p11_buffer_add (buf, value, 1); - - /* All others get encoded */ - } else { - hex[0] = '%'; - hex[1] = HEX_CHARS[((unsigned char)*value) >> 4]; - hex[2] = HEX_CHARS[((unsigned char)*value) & 0x0F]; - p11_buffer_add (buf, hex, 3); - } - - ++value; - } -} diff --git a/common/url.h b/common/url.h deleted file mode 100644 index 3c9cfb4..0000000 --- a/common/url.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2011 Collabora Ltd. - * Copyright (c) 2013 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@collabora.co.uk> - */ - -#ifndef P11_URL_H -#define P11_URL_H - -#include "buffer.h" -#include "compat.h" - -#include <stdlib.h> - -#define P11_URL_WHITESPACE " \n\r\v" - -#define P11_URL_VERBATIM "abcdefghijklmnopqrstuvwxyz" \ - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \ - "0123456789_-." - -unsigned char * p11_url_decode (const char *value, - const char *end, - const char *skip, - size_t *length); - -void p11_url_encode (const unsigned char *value, - const unsigned char *end, - const char *verbatim, - p11_buffer *buf); - -#endif /* P11_URL_H */ |