|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, Red Hat Inc. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions |
| 6 | + * are met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above |
| 9 | + * copyright notice, this list of conditions and the |
| 10 | + * following disclaimer. |
| 11 | + * * Redistributions in binary form must reproduce the |
| 12 | + * above copyright notice, this list of conditions and |
| 13 | + * the following disclaimer in the documentation and/or |
| 14 | + * other materials provided with the distribution. |
| 15 | + * * The names of contributors to this software may not be |
| 16 | + * used to endorse or promote products derived from this |
| 17 | + * software without specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 22 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 23 | + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 24 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 25 | + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 26 | + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 27 | + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 28 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 29 | + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 30 | + * DAMAGE. |
| 31 | + * |
| 32 | + * Author: Zoltan Fridrich <zfridric@redhat.com>, Daiki Ueno |
| 33 | + */ |
| 34 | + |
| 35 | +#include "config.h" |
| 36 | + |
| 37 | +#define CRYPTOKI_EXPORTS 1 |
| 38 | +#include "pkcs11.h" |
| 39 | + |
| 40 | +#include "attrs.h" |
| 41 | +#include "debug.h" |
| 42 | +#include "mock.h" |
| 43 | + |
| 44 | +#ifdef WITH_ASN1 |
| 45 | +#include "persist.h" |
| 46 | +#endif |
| 47 | + |
| 48 | +#include <stdio.h> |
| 49 | +#include <string.h> |
| 50 | + |
| 51 | +static const CK_TOKEN_INFO MOCK_TOKEN_INFO = { |
| 52 | + "PERSIST LABEL ONE ", |
| 53 | + "PERSIST MANUFACTURER ", |
| 54 | + "PERSIST MODEL ", |
| 55 | + "PERSIST SERIAL ", |
| 56 | + CKF_LOGIN_REQUIRED | CKF_USER_PIN_INITIALIZED | CKF_CLOCK_ON_TOKEN | CKF_TOKEN_INITIALIZED, |
| 57 | + 1, |
| 58 | + 2, |
| 59 | + 3, |
| 60 | + 4, |
| 61 | + 5, |
| 62 | + 6, |
| 63 | + 7, |
| 64 | + 8, |
| 65 | + 9, |
| 66 | + 10, |
| 67 | + { 75, 175 }, |
| 68 | + { 85, 185 }, |
| 69 | + { '1', '9', '9', '9', '0', '5', '2', '5', '0', '9', '1', '9', '5', '9', '0', '0' } |
| 70 | +}; |
| 71 | + |
| 72 | +static CK_RV |
| 73 | +override_C_GetTokenInfo (CK_SLOT_ID slot_id, |
| 74 | + CK_TOKEN_INFO_PTR info) |
| 75 | +{ |
| 76 | + return_val_if_fail (info != NULL, CKR_ARGUMENTS_BAD); |
| 77 | + |
| 78 | + switch (slot_id) { |
| 79 | + case MOCK_SLOT_ONE_ID: |
| 80 | + memcpy (info, &MOCK_TOKEN_INFO, sizeof (*info)); |
| 81 | + return CKR_OK; |
| 82 | + case MOCK_SLOT_TWO_ID: |
| 83 | + return CKR_TOKEN_NOT_PRESENT; |
| 84 | + default: |
| 85 | + return CKR_SLOT_ID_INVALID; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +#ifdef WITH_ASN1 |
| 90 | +static CK_RV |
| 91 | +override_C_Initialize (CK_VOID_PTR init_args) |
| 92 | +{ |
| 93 | + bool ok; |
| 94 | + size_t i, size = 0; |
| 95 | + void *data = NULL; |
| 96 | + const char *filename = "test-persist.p11-kit"; |
| 97 | + p11_mmap *map = NULL; |
| 98 | + p11_persist *persist = NULL; |
| 99 | + p11_array *objects = NULL; |
| 100 | + CK_ATTRIBUTE *attrs = NULL; |
| 101 | + CK_RV rv; |
| 102 | + |
| 103 | + map = p11_mmap_open (filename, NULL, &data, &size); |
| 104 | + if (map == NULL) |
| 105 | + return mock_C_Initialize (init_args); |
| 106 | + |
| 107 | + ok = p11_persist_magic (data, size); |
| 108 | + return_val_if_fail (ok, CKR_GENERAL_ERROR); |
| 109 | + |
| 110 | + persist = p11_persist_new (); |
| 111 | + return_val_if_fail (persist != NULL, CKR_HOST_MEMORY); |
| 112 | + |
| 113 | + objects = p11_array_new (NULL); |
| 114 | + return_val_if_fail (objects != NULL, CKR_HOST_MEMORY); |
| 115 | + |
| 116 | + ok = p11_persist_read (persist, filename, (const unsigned char *)data, size, objects); |
| 117 | + return_val_if_fail (ok, CKR_GENERAL_ERROR); |
| 118 | + |
| 119 | + rv = mock_C_Initialize (init_args); |
| 120 | + for (i = 0; i < objects->num; ++i) { |
| 121 | + attrs = p11_attrs_build (objects->elem[i], NULL); |
| 122 | + mock_module_add_object (MOCK_SLOT_ONE_ID, attrs); |
| 123 | + p11_attrs_free (attrs); |
| 124 | + } |
| 125 | + |
| 126 | + p11_array_free (objects); |
| 127 | + p11_persist_free (persist); |
| 128 | + p11_mmap_close (map); |
| 129 | + return rv; |
| 130 | +} |
| 131 | + |
| 132 | +struct WriteData { |
| 133 | + p11_persist *persist; |
| 134 | + p11_buffer buf; |
| 135 | +}; |
| 136 | + |
| 137 | +static bool |
| 138 | +persist_enumerator (CK_OBJECT_HANDLE handle, |
| 139 | + CK_ATTRIBUTE *attrs, |
| 140 | + void *user_data) |
| 141 | +{ |
| 142 | + struct WriteData *data = user_data; |
| 143 | + |
| 144 | + p11_persist_write (data->persist, attrs, &data->buf); |
| 145 | + |
| 146 | + return true; |
| 147 | +} |
| 148 | + |
| 149 | +static CK_RV |
| 150 | +override_C_Finalize (CK_VOID_PTR reserved) |
| 151 | +{ |
| 152 | + bool ok; |
| 153 | + FILE *f = NULL; |
| 154 | + const char *filename = "test-persist.p11-kit"; |
| 155 | + CK_SESSION_HANDLE session = 0; |
| 156 | + struct WriteData data; |
| 157 | + CK_RV rv; |
| 158 | + |
| 159 | + ok = p11_buffer_init (&data.buf, 0); |
| 160 | + return_val_if_fail (ok, CKR_HOST_MEMORY); |
| 161 | + |
| 162 | + data.persist = p11_persist_new (); |
| 163 | + return_val_if_fail (data.persist != NULL, CKR_HOST_MEMORY); |
| 164 | + |
| 165 | + rv = mock_C_OpenSession (MOCK_SLOT_ONE_ID, CKF_SERIAL_SESSION, NULL, NULL, &session); |
| 166 | + return_val_if_fail (rv == CKR_OK, CKR_GENERAL_ERROR); |
| 167 | + |
| 168 | + mock_module_enumerate_objects (session, |
| 169 | + persist_enumerator, |
| 170 | + &data); |
| 171 | + |
| 172 | + rv = mock_C_CloseSession (session); |
| 173 | + return_val_if_fail (rv == CKR_OK, CKR_GENERAL_ERROR); |
| 174 | + |
| 175 | + f = fopen (filename, "wb"); |
| 176 | + return_val_if_fail (f != NULL, CKR_HOST_MEMORY); |
| 177 | + fwrite (data.buf.data, 1, data.buf.len, f); |
| 178 | + fclose (f); |
| 179 | + |
| 180 | + p11_persist_free (data.persist); |
| 181 | + p11_buffer_uninit (&data.buf); |
| 182 | + return mock_C_Finalize (reserved); |
| 183 | +} |
| 184 | +#endif /* WITH_ASN1 */ |
| 185 | + |
| 186 | +#ifdef OS_WIN32 |
| 187 | +__declspec(dllexport) |
| 188 | +#endif |
| 189 | +CK_RV |
| 190 | +C_GetFunctionList (CK_FUNCTION_LIST_PTR_PTR list) |
| 191 | +{ |
| 192 | + mock_module_init (); |
| 193 | +#ifdef WITH_ASN1 |
| 194 | + mock_module.C_Initialize = override_C_Initialize; |
| 195 | + mock_module.C_Finalize = override_C_Finalize; |
| 196 | +#endif |
| 197 | + mock_module.C_GetFunctionList = C_GetFunctionList; |
| 198 | + mock_module.C_GetTokenInfo = override_C_GetTokenInfo; |
| 199 | + if (list == NULL) |
| 200 | + return CKR_ARGUMENTS_BAD; |
| 201 | + *list = &mock_module; |
| 202 | + return CKR_OK; |
| 203 | +} |
0 commit comments