/* * rpc_hash.c * ---------- * Remote procedure call server-side hash 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 #include "hal.h" #include "hal_internal.h" /* * Need table and handle allocation, including some kind of in_use * flag (perhaps just handle == none). * * Hash and HMAC aren't really things for which we need permission * bits, so not sure we even care about login stuff here. */ typedef struct { hal_client_handle_t client_handle; hal_session_handle_t session_handle; hal_hash_handle_t hash_handle; union { hal_hash_state_t *hash; hal_hmac_state_t *hmac; } state; } handle_slot_t; #ifndef HAL_STATIC_HASH_STATE_BLOCKS #define HAL_STATIC_HASH_STATE_BLOCKS 0 #endif #ifndef HAL_STATIC_HMAC_STATE_BLOCKS #define HAL_STATIC_HMAC_STATE_BLOCKS 0 #endif #if HAL_STATIC_HASH_STATE_BLOCKS > 0 static handle_slot_t hash_handle[HAL_STATIC_HASH_STATE_BLOCKS]; #endif #if HAL_STATIC_HMAC_STATE_BLOCKS > 0 static handle_slot_t hmac_handle[HAL_STATIC_HMAC_STATE_BLOCKS]; #endif /* * Handle allocation is simple: we look for an unused (state == NULL) * slot in the appropriate table, and, assuming we find one, construct * a composite handle consisting of a flag telling us which table this * is, the index into the table, and a counter whose sole purpose is * to keep the same handle from reoccurring anytime soon, to help * identify use-after-free bugs in calling code. */ #define HANDLE_FLAG_HMAC 0x80000000 static inline handle_slot_t *alloc_handle(const int is_hmac) { #if HAL_STATIC_HASH_STATE_BLOCKS > 0 || HAL_STATIC_HMAC_STATE_BLOCKS > 0 static uint16_t next_glop = 0; uint32_t glop = ++next_glop << 16; next_glop %= 0x7FFF; #endif #if HAL_STATIC_HASH_STATE_BLOCKS > 0 if (!is_hmac) { for (int i = 0; i < sizeof(hash_handle)/sizeof(*hash_handle); i++) { if (hash_handle[i].state.hash != NULL) continue; hash_handle[i].hash_handle.handle = i | glop; return &hash_handle[i]; } } #endif #if HAL_STATIC_HMAC_STATE_BLOCKS > 0 if (is_hmac) { for (int i = 0; i < sizeof(hmac_handle)/sizeof(*hmac_handle); i++) { if (hmac_handle[i].state.hmac != NULL) continue; hmac_handle[i].hash_handle.handle = i | glop | HANDLE_FLAG_HMAC; return &hmac_handle[i]; } } #endif return NULL; } /* * Check a caller-supplied handle. Must be in range, in use, and have * the right glop. Returns slot pointer on success, NULL otherwise. */ static inline handle_slot_t *find_handle(const hal_hash_handle_t handle) { #if HAL_STATIC_HASH_STATE_BLOCKS > 0 || HAL_STATIC_HMAC_STATE_BLOCKS > 0 const int i = (int) (handle.handle & 0xFFFF); const int is_hmac = (handle.handle & HANDLE_FLAG_HMAC) != 0; #endif #if HAL_STATIC_HASH_STATE_BLOCKS > 0 if (!is_hmac && i < sizeof(hash_handle)/sizeof(*hash_handle) && hash_handle[i].hash_handle.handle == handle.handle && hash_handle[i].state.hash != NULL) return &hash_handle[i]; #endif #if HAL_STATIC_HMAC_STATE_BLOCKS > 0 if (is_hmac && i < sizeof(hmac_handle)/sizeof(*hmac_handle) && hmac_handle[i].hash_handle.handle == handle.handle && hmac_handle[i].state.hmac != NULL) return &hmac_handle[i]; #endif return NULL; } static inline void free_handle(handle_slot_t *slot) { if (slot != NULL) /* state is a union, so this this works for hash and hmac */ slot->state.hash = NULL; } /* * Translate an algorithm number to a descriptor. */ static inline const hal_hash_descriptor_t *alg_to_descriptor(const hal_digest_algorithm_t alg) { switch (alg) { case HAL_DIGEST_ALGORITHM_SHA1: return hal_hash_sha1; case HAL_DIGEST_ALGORITHM_SHA256: return hal_hash_sha256; case HAL_DIGEST_ALGORITHM_SHA512_224: return hal_hash_sha512_224; case HAL_DIGEST_ALGORITHM_SHA512_256: return hal_hash_sha512_256; case HAL_DIGEST_ALGORITHM_SHA384: return hal_hash_sha384; case HAL_DIGEST_ALGORITHM_SHA512: return hal_hash_sha512; default: return NULL; } } /* * Given a slot pointer, fetch the descriptor. */ static inline const hal_hash_descriptor_t *slot_to_descriptor(const handle_slot_t * const slot) { if (slot == NULL) return NULL; if ((slot->hash_handle.handle & HANDLE_FLAG_HMAC) == 0) return hal_hash_get_descriptor(slot->state.hash); else return hal_hmac_get_descriptor(slot->state.hmac); } /* * Public API */ static hal_error_t get_digest_length(const hal_digest_algorithm_t alg, size_t *lengt
/*
 * rpc_client_loopback.c
 * ---------------------
 * Remote procedure call transport over loopback socket.
 *
 * 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 <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>     /* close */

#include "hal.h"
#include "hal_internal.h"

static int sock = -1;

hal_error_t hal_rpc_client_transport_init(void)
{
    struct sockaddr_in sin;

    sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock == -1)
        return perror("socket"), HAL_ERROR_RPC_TRANSPORT;
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    sin.sin_port = 17425;
    if (connect(sock, (const struct sockaddr *)&sin, sizeof(sin)) != 0)
        return perror("connect"), HAL_ERROR_RPC_TRANSPORT;
    return HAL_OK;
}

hal_error_t hal_rpc_client_transport_close(void)
{
    int ret = close(sock);
    sock = -1;
    if (ret != 0)
        return perror("close"), HAL_ERROR_RPC_TRANSPORT;
    return HAL_OK;
}

hal_error_t hal_rpc_send(const uint8_t * const buf, const size_t len)
{
    if (send(sock, buf, len, 0) == -1)
        return perror("send"), HAL_ERROR_RPC_TRANSPORT;
    return HAL_OK;
}

hal_error_t hal_rpc_recv(uint8_t * const buf, size_t * const len)
{
    int ret;

    if ((ret = recv(sock, buf, *len, 0)) == -1)
        return HAL_ERROR_RPC_TRANSPORT;
    *len = ret;
    return HAL_OK;
}