/* * hal_rpc.c * --------- * Remote procedure call public API implementation. * * Authors: Rob Austein * Copyright (c) 2015-2016, NORDUnet A/S All rights reserved. * * 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. * * - Neither the name of the NORDUnet 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 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 * HOLDER 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 "hal.h" #include "hal_internal.h" const hal_hash_handle_t hal_hash_handle_none = {HAL_HANDLE_NONE}; /* * PIN lengths. These are somewhat arbitrary, and the current values * are really placeholders until we figure out something better. * Minimum length here is almost certainly too short for production * use, we allow it because most test programs fail if we insist on a * PIN long enough to have any real security. */ #ifndef HAL_PIN_MINIMUM_LENGTH #define HAL_PIN_MINIMUM_LENGTH 4 #endif #ifndef HAL_PIN_MAXIMUM_LENGTH #define HAL_PIN_MAXIMUM_LENGTH 4096 #endif const size_t hal_rpc_min_pin_length = HAL_PIN_MINIMUM_LENGTH; const size_t hal_rpc_max_pin_length = HAL_PIN_MAXIMUM_LENGTH; static inline int check_pkey_type(const hal_key_type_t type) { switch (type) { case HAL_KEY_TYPE_RSA_PRIVATE: case HAL_KEY_TYPE_RSA_PUBLIC: case HAL_KEY_TYPE_EC_PRIVATE: case HAL_KEY_TYPE_EC_PUBLIC: return 1; default: return 0; } } static inline int check_pkey_flags(const hal_key_flags_t flags) { return (flags &~ (HAL_KEY_FLAG_USAGE_DIGITALSIGNATURE | HAL_KEY_FLAG_USAGE_KEYENCIPHERMENT | HAL_KEY_FLAG_USAGE_DATAENCIPHERMENT | HAL_KEY_FLAG_TOKEN | HAL_KEY_FLAG_PUBLIC | HAL_KEY_FLAG_EXPORTABLE)) == 0; } static inline int check_pkey_type_curve_flags(const hal_key_type_t type, const hal_curve_name_t curve, const hal_key_flags_t flags) { if (!check_pkey_flags(flags)) return 0; switch (type) { case HAL_KEY_TYPE_RSA_PRIVATE: case HAL_KEY_TYPE_RSA_PUBLIC: return curve == HAL_CURVE_NONE; case HAL_KEY_TYPE_EC_PRIVATE: case HAL_KEY_TYPE_EC_PUBLIC: switch (curve) { case HAL_CURVE_P256: case HAL_CURVE_P384: case HAL_CURVE_P521: return 1; default: return 0; } default: return 0; } } hal_error_t hal_rpc_get_version(uint32_t *version) { return hal_rpc_misc_dispatch->get_version(version); } hal_error_t hal_rpc_get_random(void *buffer, const size_t length) { if (buffer == NULL) return HAL_ERROR_BAD_ARGUMENTS; if (length == 0) return HAL_OK; return hal_rpc_misc_dispatch->get_random(buffer, length); } hal_error_t hal_rpc_set_pin(const hal_client_handle_t client, const hal_user_t user, const char * const newpin, const size_t newpin_len) { if (newpin == NULL || newpin_len < hal_rpc_min_pin_length || newpin_len > hal_rpc_max_pin_length || (user != HAL_USER_NORMAL && user != HAL_USER_SO && user != HAL_USER_WHEEL)) return HAL_ERROR_BAD_ARGUMENTS; return hal_rpc_misc_dispatch->set_pin(client, user, newpin, newpin_len); } hal_error_t hal_rpc_login(const hal_client_handle_t client, const hal_user_t user, const char * const pin, const size_t pin_len) { if (pin == NULL || pin_len < hal_rpc_min_pin_length || pin_len > hal_rpc_max_pin_length || (user != HAL_USER_NORMAL && user != HAL_USER_SO && user != HAL_USER_WHEEL)) return HAL_ERROR_BAD_ARGUMENTS; return hal_rpc_misc_dispatch->login(client, user, pin, pin_len); } hal_error_t hal_rpc_logout(const hal_client_handle_t client) { return hal_rpc_misc_dispatch->logout(client); } hal_error_t hal_rpc_logout_all(void) { return hal_rpc_misc_dispatch->logout_all(); } hal_error_t hal_rpc_is_logged_in(const hal_client_handle_t client, const hal_user_t user) { if (user != HAL_USER_NORMAL && user != HAL_USER_SO && user != HAL_USER_WHEEL) return HAL_ERROR_BAD_ARGUMENTS; return hal_rpc_misc_dispatch->is_logged_in(client, user); } hal_error_t hal_rpc_hash_get_digest_length(const hal_digest_algorithm_t alg, size_t *length) { if (length == NULL) return HAL_ERROR_BAD_ARGUMENTS; return hal_rpc_hash_dispatch->get_digest_length(alg, length); } hal_error_t hal_rpc_hash_get_digest_algorithm_id(const hal_digest_algorithm_t alg, uint8_t *id, size_t *len, const size_t len_max) { return hal_rpc_hash_dispatch->get_digest_algorithm_id(alg, id, len, len_max); } hal_error_t hal_rpc_hash_get_algorithm(const hal_hash_handle_t hash, hal_digest_algorithm_t *alg) { if (hash.handle == HAL_HANDLE_NONE || alg == NULL) return HAL_ERROR_BAD_ARGUMENTS; return hal_rpc_hash_dispatch->get_algorithm(hash, alg); } hal_error_t hal_rpc_hash_initialize(const hal_client_handle_t client, const hal_session_handle_t session, hal_hash_handle_t *hash, const hal_digest_algorithm_t alg, const uint8_t * const key, const size_t key_len) { if (hash == NULL) return HAL_ERROR_
/*
 * test-rpc_login.c
 * ----------------
 * Test code for RPC interface to Cryptech hash cores.
 *
 * Copyright (c) 2016, NORDUnet A/S
 * All rights reserved.
 *
 * 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.
 *
 * - Neither the name of the NORDUnet 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 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
 * HOLDER 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 <stdio.h>
#include <string.h>
#include <strings.h>

#include <hal.h>

#define check(op)                                               \
    do {                                                        \
        hal_error_t err = (op);                                 \
        if (err) {                                              \
            printf("%s: %s\n", #op, hal_error_string(err));     \
            return err;                                         \
        }                                                       \
    } while (0)

int main(int argc, char *argv[])
{
    hal_client_handle_t client = {0};
    hal_user_t user = HAL_USER_WHEEL;

    if (argc < 3) {
        printf("usage: %s user pin\n", argv[0]);
        return 1;
    }

    if (strcasecmp(argv[1], "wheel") == 0)
        user = HAL_USER_WHEEL;
    else if (strcasecmp(argv[1], "so") == 0)
        user = HAL_USER_SO;
    else if (strcasecmp(argv[1], "user") == 0)
        user = HAL_USER_NORMAL;
    else {
        printf("user name must be one of 'wheel', 'so', or 'user'\n");
        return 1;
    }

    check(hal_rpc_client_init());

    check(hal_rpc_login(client, user, argv[2], strlen(argv[2])));
    check(hal_rpc_is_logged_in(client, user));
    check(hal_rpc_logout(client));

    check(hal_rpc_client_close());
    return 0;
}