"Fossies" - the Fresh Open Source Software Archive

Member "opensc-0.22.0/src/tests/p11test/p11test_helpers.c" (10 Aug 2021, 5062 Bytes) of package /linux/privat/opensc-0.22.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "p11test_helpers.c": 0.19.0_vs_0.20.0.

    1 /*
    2  * p11test_helpers.c: Test suite for PKCS#11 API: Supporting functions
    3  *
    4  * Copyright (C) 2016 Martin Strhársky <strharsky.martin@gmail.com>
    5  * Copyright (C) 2016, 2017 Red Hat, Inc.
    6  *
    7  * Author: Jakub Jelen <jjelen@redhat.com>
    8  *
    9  * This library is free software; you can redistribute it and/or
   10  * modify it under the terms of the GNU Lesser General Public
   11  * License as published by the Free Software Foundation; either
   12  * version 2.1 of the License, or (at your option) any later version.
   13  *
   14  * This library is distributed in the hope that it will be useful,
   15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   17  * Lesser General Public License for more details.
   18  *
   19  * You should have received a copy of the GNU General Public License
   20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   21  */
   22 
   23 #include "p11test_helpers.h"
   24 #include "p11test_loader.h"
   25 
   26 int
   27 open_session(token_info_t *info)
   28 {
   29     CK_FUNCTION_LIST_PTR function_pointer = info->function_pointer;
   30     CK_RV rv;
   31 
   32     rv = function_pointer->C_OpenSession(info->slot_id,
   33         CKF_SERIAL_SESSION | CKF_RW_SESSION, NULL_PTR, NULL_PTR,
   34         &info->session_handle);
   35 
   36     if (rv != CKR_OK) {
   37         return 1;
   38     }
   39 
   40     debug_print("Session was successfully created");
   41     return 0;
   42 }
   43 
   44 int
   45 initialize_cryptoki(token_info_t *info)
   46 {
   47     CK_FUNCTION_LIST_PTR function_pointer = info->function_pointer;
   48     CK_RV rv;
   49 
   50     rv = function_pointer->C_Initialize(NULL_PTR);
   51     if (rv != CKR_OK) {
   52         fprintf(stderr, "Could not initialize CRYPTOKI!\n");
   53         return 1;
   54     }
   55 
   56     if (get_slot_with_card(info)) {
   57         function_pointer->C_Finalize(NULL_PTR);
   58         fprintf(stderr, "There is no card present in reader.\n");
   59         return 1;
   60     }
   61 
   62     return 0;
   63 }
   64 
   65 int token_initialize(void **state)
   66 {
   67     token_info_t *info = (token_info_t *) *state;
   68     if (initialize_cryptoki(info)) {
   69         debug_print("CRYPTOKI couldn't be initialized");
   70         return 1;
   71     }
   72     return 0;
   73 }
   74 
   75 void logfile_init(token_info_t *info)
   76 {
   77     if (token.log.outfile == NULL) {
   78         return;
   79     }
   80 
   81     if ((info->log.fd = fopen(token.log.outfile, "w")) == NULL) {
   82         fail_msg("Couldn't open file for test results.");
   83         exit(1);
   84     }
   85     fprintf(info->log.fd, "{\n\"time\": 0,\n\"results\": [");
   86     info->log.in_test = 0;
   87     info->log.first = 1;
   88 }
   89 
   90 void logfile_finalize(token_info_t *info)
   91 {
   92     if (info == NULL || info->log.fd == NULL) {
   93         return;
   94     }
   95 
   96     /* Make sure the JSON object for test is closed */
   97     if (info->log.in_test) {
   98         fprintf(info->log.fd, ",\n\t\"result\": \"unknown\"\n},");
   99         info->log.in_test = 0;
  100     }
  101 
  102     fprintf(info->log.fd, "]\n}\n");
  103     fclose(info->log.fd);
  104 }
  105 
  106 int group_setup(void **state)
  107 {
  108     token_info_t * info = calloc(sizeof(token_info_t), 1);
  109 
  110     assert_non_null(info);
  111 
  112     info->pin = token.pin;
  113     info->pin_length = token.pin_length;
  114     info->interactive = token.interactive;
  115     info->slot_id = token.slot_id;
  116 
  117     if (load_pkcs11_module(info, token.library_path)) {
  118         free(info);
  119         fail_msg("Could not load module!\n");
  120         exit(1);
  121     }
  122 
  123     logfile_init(info);
  124 
  125     *state = info;
  126     return 0;
  127 }
  128 
  129 int group_teardown(void **state)
  130 {
  131     token_info_t *info = (token_info_t *) *state;
  132     debug_print("Clearing state after group tests!");
  133     // XXX do not finalize already Finalized
  134     //if(info && info->function_pointer)
  135     //  info->function_pointer->C_Finalize(NULL_PTR);
  136 
  137     free(token.library_path);
  138     free(token.pin);
  139 
  140     logfile_finalize(info);
  141     free(info);
  142 
  143     close_pkcs11_module();
  144 
  145     return 0;
  146 }
  147 
  148 int prepare_token(token_info_t *info)
  149 {
  150     if (initialize_cryptoki(info)) {
  151         debug_print("CRYPTOKI couldn't be initialized");
  152         return 1;
  153     }
  154 
  155     if (open_session(info)) {
  156         debug_print("Could not open session to token!");
  157         return 1;
  158     }
  159 
  160     return 0;
  161 }
  162 
  163 int finalize_token(token_info_t *info)
  164 {
  165     CK_FUNCTION_LIST_PTR function_pointer = info->function_pointer;
  166 
  167     info->session_handle = 0;
  168     debug_print("Closing all sessions");
  169     function_pointer->C_CloseAllSessions(info->slot_id);
  170     debug_print("Finalize CRYPTOKI");
  171     function_pointer->C_Finalize(NULL_PTR);
  172     return 0;
  173 }
  174 
  175 int user_login_setup(void **state)
  176 {
  177     token_info_t *info = (token_info_t *) *state;
  178     CK_FUNCTION_LIST_PTR function_pointer = info->function_pointer;
  179     CK_RV rv;
  180 
  181     if (prepare_token(info)) {
  182         fail_msg("Could not prepare token.\n");
  183         exit(1);
  184     }
  185 
  186     debug_print("Logging in to the token!");
  187     rv = function_pointer->C_Login(info->session_handle, CKU_USER,
  188         token.pin, token.pin_length);
  189 
  190     if (rv != CKR_OK) {
  191         fail_msg("Could not login to token with user PIN '%s'\n", token.pin);
  192         exit(1);
  193     }
  194 
  195     return 0;
  196 }
  197 
  198 int after_test_cleanup(void **state)
  199 {
  200     token_info_t *info = (token_info_t *) *state;
  201     CK_FUNCTION_LIST_PTR function_pointer = info->function_pointer;
  202 
  203     debug_print("Logging out from token");
  204     function_pointer->C_Logout(info->session_handle);
  205 
  206     finalize_token(info);
  207     return 0;
  208 }
  209 
  210 int token_setup(void **state)
  211 {
  212     token_info_t *info = (token_info_t *) *state;
  213 
  214     if (prepare_token(info)) {
  215         fail_msg("Could not prepare token.\n");
  216         exit(1);
  217     }
  218 
  219     return 0;
  220 }
  221 
  222 int token_cleanup(void **state)
  223 {
  224     token_info_t *info = (token_info_t *) *state;
  225 
  226     finalize_token(info);
  227     return 0;
  228 }
  229