diff options
author | Rob Austein <sra@hactrn.net> | 2016-07-07 16:48:23 -0400 |
---|---|---|
committer | Rob Austein <sra@hactrn.net> | 2016-07-07 16:48:23 -0400 |
commit | db595b9c0c69c0dc43aeb78ecc16f8e3f6f058f3 (patch) | |
tree | 78cfcad1d65afd9db1e1b244edfafd3df9b49a4f | |
parent | c5bbdc94037efa5050ad32454509bc1c366c288f (diff) |
Drop RPC response message if the opcode doesn't match what we sent.
-rw-r--r-- | hal_internal.h | 4 | ||||
-rw-r--r-- | rpc_client.c | 288 | ||||
-rw-r--r-- | rpc_server.c | 4 |
3 files changed, 88 insertions, 208 deletions
diff --git a/hal_internal.h b/hal_internal.h index 129dc31..1c06494 100644 --- a/hal_internal.h +++ b/hal_internal.h @@ -432,8 +432,8 @@ typedef enum { RPC_FUNC_PKEY_GET_KEY_FLAGS, RPC_FUNC_PKEY_GET_PUBLIC_KEY_LEN, RPC_FUNC_PKEY_GET_PUBLIC_KEY, - RPC_FUNC_PKEY_REMOTE_SIGN, - RPC_FUNC_PKEY_REMOTE_VERIFY, + RPC_FUNC_PKEY_SIGN, + RPC_FUNC_PKEY_VERIFY, RPC_FUNC_PKEY_LIST, RPC_FUNC_PKEY_RENAME, } rpc_func_num_t; diff --git a/rpc_client.c b/rpc_client.c index 4c2a6c7..a517528 100644 --- a/rpc_client.c +++ b/rpc_client.c @@ -39,12 +39,16 @@ #include "hal_internal.h" #include "xdr_internal.h" -/* - * RPC calls. - */ +#ifndef HAL_RPC_CLIENT_DEBUG +#define HAL_RPC_CLIENT_DEBUG 0 +#endif +#if HAL_RPC_CLIENT_DEBUG #include <stdio.h> #define check(op) do { const hal_error_t _err_ = (op); if (_err_ != HAL_OK) { printf("%s returned %d (%s)\n", #op, _err_, hal_error_string(_err_)); return _err_; } } while (0) +#else +#define check(op) do { const hal_error_t _err_ = (op); if (_err_ != HAL_OK) { return _err_; } } while (0) +#endif #define pad(n) (((n) + 3) & ~3) @@ -52,13 +56,58 @@ #if RPC_CLIENT != RPC_CLIENT_LOCAL +/* + * Consolidate a bit of the repetitive code from the packet receive loop. + * We're looking for a packet which is a response to the packet we sent, + * so if the opcode is wrong, we discard and wait for another packet. + */ + +static hal_error_t read_matching_packet(const rpc_func_num_t expected_func, + uint8_t *inbuf, + const size_t inbuf_max, + const uint8_t **iptr, + const uint8_t **ilimit) +{ + hal_client_handle_t dummy_client; + uint32_t received_func; + size_t ilen = inbuf_max; + hal_error_t err; + + assert(inbuf != NULL && iptr != NULL && ilimit != NULL); + + do { + + if ((err = hal_rpc_recv(inbuf, &ilen)) != HAL_OK) + return err; + + assert(ilen <= inbuf_max); + *iptr = inbuf; + *ilimit = inbuf + ilen; + + if ((err = hal_xdr_decode_int(iptr, *ilimit, &received_func)) == HAL_ERROR_XDR_BUFFER_OVERFLOW) + continue; + if (err != HAL_OK) + return err; + + if ((err = hal_xdr_decode_int(iptr, *ilimit, &dummy_client.handle)) == HAL_ERROR_XDR_BUFFER_OVERFLOW) + continue; + if (err != HAL_OK) + return err; + + } while (received_func != expected_func); + + return HAL_OK; +} + +/* + * RPC calls. + */ + static hal_error_t get_version(uint32_t *version) { uint8_t outbuf[nargs(2)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(4)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); - uint32_t rpc_func_num; hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; @@ -66,11 +115,7 @@ static hal_error_t get_version(uint32_t *version) check(hal_xdr_encode_int(&optr, olimit, dummy_client.handle)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_GET_VERSION, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); if (rpc_ret == HAL_OK) { @@ -84,9 +129,7 @@ static hal_error_t get_random(void *buffer, const size_t length) uint8_t outbuf[nargs(3)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(4) + pad(length)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); uint32_t rcvlen = length; - uint32_t rpc_func_num; hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; @@ -95,11 +138,7 @@ static hal_error_t get_random(void *buffer, const size_t length) check(hal_xdr_encode_int(&optr, olimit, (uint32_t)length)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_GET_RANDOM, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); if (rpc_ret == HAL_OK) { @@ -116,9 +155,6 @@ static hal_error_t set_pin(const hal_client_handle_t client, uint8_t outbuf[nargs(4) + pad(pin_len)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(3)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); - uint32_t rpc_func_num; - hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; check(hal_xdr_encode_int(&optr, olimit, RPC_FUNC_SET_PIN)); @@ -127,11 +163,7 @@ static hal_error_t set_pin(const hal_client_handle_t client, check(hal_xdr_encode_buffer(&optr, olimit, (const uint8_t *)pin, pin_len)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_SET_PIN, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); return rpc_ret; @@ -158,9 +190,6 @@ static hal_error_t login(const hal_client_handle_t client, uint8_t outbuf[nargs(4) + pad(pin_len)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(3)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); - uint32_t rpc_func_num; - hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; check(hal_xdr_encode_int(&optr, olimit, RPC_FUNC_LOGIN)); @@ -169,11 +198,7 @@ static hal_error_t login(const hal_client_handle_t client, check(hal_xdr_encode_buffer(&optr, olimit, (const uint8_t *)pin, pin_len)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_LOGIN, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); return rpc_ret; @@ -184,20 +209,13 @@ static hal_error_t logout(const hal_client_handle_t client) uint8_t outbuf[nargs(2)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(3)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); - uint32_t rpc_func_num; - hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; check(hal_xdr_encode_int(&optr, olimit, RPC_FUNC_LOGOUT)); check(hal_xdr_encode_int(&optr, olimit, client.handle)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_LOGOUT, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); return rpc_ret; @@ -208,8 +226,6 @@ static hal_error_t logout_all(void) uint8_t outbuf[nargs(2)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(3)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); - uint32_t rpc_func_num; hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; @@ -217,11 +233,7 @@ static hal_error_t logout_all(void) check(hal_xdr_encode_int(&optr, olimit, dummy_client.handle)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_LOGOUT_ALL, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); return rpc_ret; @@ -233,9 +245,6 @@ static hal_error_t is_logged_in(const hal_client_handle_t client, uint8_t outbuf[nargs(3)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(3)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); - uint32_t rpc_func_num; - hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; check(hal_xdr_encode_int(&optr, olimit, RPC_FUNC_IS_LOGGED_IN)); @@ -243,11 +252,7 @@ static hal_error_t is_logged_in(const hal_client_handle_t client, check(hal_xdr_encode_int(&optr, olimit, user)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_IS_LOGGED_IN, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); return rpc_ret; @@ -258,9 +263,7 @@ static hal_error_t hash_get_digest_len(const hal_digest_algorithm_t alg, size_t uint8_t outbuf[nargs(3)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(4)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); uint32_t len32; - uint32_t rpc_func_num; hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; @@ -269,11 +272,7 @@ static hal_error_t hash_get_digest_len(const hal_digest_algorithm_t alg, size_t check(hal_xdr_encode_int(&optr, olimit, alg)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_HASH_GET_DIGEST_LEN, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); if (rpc_ret == HAL_OK) { @@ -289,9 +288,7 @@ static hal_error_t hash_get_digest_algorithm_id(const hal_digest_algorithm_t alg uint8_t outbuf[nargs(4)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(4) + pad(len_max)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); uint32_t len32 = len_max; - uint32_t rpc_func_num; hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; @@ -301,11 +298,7 @@ static hal_error_t hash_get_digest_algorithm_id(const hal_digest_algorithm_t alg check(hal_xdr_encode_int(&optr, olimit, len_max)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_HASH_GET_DIGEST_LEN, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); if (rpc_ret == HAL_OK) { @@ -320,9 +313,7 @@ static hal_error_t hash_get_algorithm(const hal_hash_handle_t hash, hal_digest_a uint8_t outbuf[nargs(3)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(4)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); uint32_t alg32; - uint32_t rpc_func_num; hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; @@ -331,16 +322,12 @@ static hal_error_t hash_get_algorithm(const hal_hash_handle_t hash, hal_digest_a check(hal_xdr_encode_int(&optr, olimit, hash.handle)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_HASH_GET_ALGORITHM, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); if (rpc_ret == HAL_OK) { check(hal_xdr_decode_int(&iptr, ilimit, &alg32)); - *alg = (hal_digest_algorithm_t)alg32; + *alg = (hal_digest_algorithm_t) alg32; } return rpc_ret; } @@ -354,9 +341,6 @@ static hal_error_t hash_initialize(const hal_client_handle_t client, uint8_t outbuf[nargs(5) + pad(key_len)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(4)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); - uint32_t rpc_func_num; - hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; check(hal_xdr_encode_int(&optr, olimit, RPC_FUNC_HASH_INITIALIZE)); @@ -366,11 +350,7 @@ static hal_error_t hash_initialize(const hal_client_handle_t client, check(hal_xdr_encode_buffer(&optr, olimit, key, key_len)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_HASH_INITIALIZE, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); if (rpc_ret == HAL_OK) { @@ -385,8 +365,6 @@ static hal_error_t hash_update(const hal_hash_handle_t hash, uint8_t outbuf[nargs(4) + pad(length)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(3)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); - uint32_t rpc_func_num; hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; @@ -396,11 +374,7 @@ static hal_error_t hash_update(const hal_hash_handle_t hash, check(hal_xdr_encode_buffer(&optr, olimit, data, length)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_HASH_UPDATE, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); return rpc_ret; @@ -412,9 +386,7 @@ static hal_error_t hash_finalize(const hal_hash_handle_t hash, uint8_t outbuf[nargs(4)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(4) + pad(length)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); uint32_t digest_len = length; - uint32_t rpc_func_num; hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; @@ -424,11 +396,7 @@ static hal_error_t hash_finalize(const hal_hash_handle_t hash, check(hal_xdr_encode_int(&optr, olimit, length)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_HASH_FINALIZE, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); if (rpc_ret == HAL_OK) { @@ -450,9 +418,6 @@ static hal_error_t pkey_remote_load(const hal_client_handle_t client, uint8_t outbuf[nargs(8) + pad(name_len) + pad(der_len)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(4)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); - uint32_t rpc_func_num; - hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; check(hal_xdr_encode_int(&optr, olimit, RPC_FUNC_PKEY_LOAD)); @@ -465,11 +430,7 @@ static hal_error_t pkey_remote_load(const hal_client_handle_t client, check(hal_xdr_encode_int(&optr, olimit, flags)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_PKEY_LOAD, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); if (rpc_ret == HAL_OK) @@ -488,9 +449,6 @@ static hal_error_t pkey_remote_find(const hal_client_handle_t client, uint8_t outbuf[nargs(6) + pad(name_len)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(4)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); - uint32_t rpc_func_num; - hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; check(hal_xdr_encode_int(&optr, olimit, RPC_FUNC_PKEY_FIND)); @@ -501,11 +459,7 @@ static hal_error_t pkey_remote_find(const hal_client_handle_t client, check(hal_xdr_encode_int(&optr, olimit, flags)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_PKEY_FIND, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); if (rpc_ret == HAL_OK) @@ -525,9 +479,6 @@ static hal_error_t pkey_remote_generate_rsa(const hal_client_handle_t client, uint8_t outbuf[nargs(7) + pad(name_len) + pad(exp_len)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(4)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); - uint32_t rpc_func_num; - hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; check(hal_xdr_encode_int(&optr, olimit, RPC_FUNC_PKEY_GENERATE_RSA)); @@ -539,11 +490,7 @@ static hal_error_t pkey_remote_generate_rsa(const hal_client_handle_t client, check(hal_xdr_encode_int(&optr, olimit, flags)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_PKEY_GENERATE_RSA, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); if (rpc_ret == HAL_OK) @@ -562,9 +509,6 @@ static hal_error_t pkey_remote_generate_ec(const hal_client_handle_t client, uint8_t outbuf[nargs(6) + pad(name_len)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(4)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); - uint32_t rpc_func_num; - hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; check(hal_xdr_encode_int(&optr, olimit, RPC_FUNC_PKEY_GENERATE_EC)); @@ -575,11 +519,7 @@ static hal_error_t pkey_remote_generate_ec(const hal_client_handle_t client, check(hal_xdr_encode_int(&optr, olimit, flags)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_PKEY_GENERATE_EC, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); if (rpc_ret == HAL_OK) @@ -593,8 +533,6 @@ static hal_error_t pkey_remote_close(const hal_pkey_handle_t pkey) uint8_t outbuf[nargs(3)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(3)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); - uint32_t rpc_func_num; hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; @@ -603,11 +541,7 @@ static hal_error_t pkey_remote_close(const hal_pkey_handle_t pkey) check(hal_xdr_encode_int(&optr, olimit, pkey.handle)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_PKEY_CLOSE, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); return rpc_ret; @@ -618,8 +552,6 @@ static hal_error_t pkey_remote_delete(const hal_pkey_handle_t pkey) uint8_t outbuf[nargs(3)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(3)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); - uint32_t rpc_func_num; hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; @@ -628,11 +560,7 @@ static hal_error_t pkey_remote_delete(const hal_pkey_handle_t pkey) check(hal_xdr_encode_int(&optr, olimit, pkey.handle)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_PKEY_DELETE, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); return rpc_ret; @@ -644,8 +572,6 @@ static hal_error_t pkey_remote_rename(const hal_pkey_handle_t pkey, uint8_t outbuf[nargs(4) + pad(name_len)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(3)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); - uint32_t rpc_func_num; hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; @@ -655,11 +581,7 @@ static hal_error_t pkey_remote_rename(const hal_pkey_handle_t pkey, check(hal_xdr_encode_buffer(&optr, olimit, name, name_len)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_PKEY_RENAME, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); return rpc_ret; @@ -672,9 +594,7 @@ static hal_error_t pkey_remote_get_key_type(const hal_pkey_handle_t pkey, uint8_t outbuf[nargs(3)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(4)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); uint32_t type32; - uint32_t rpc_func_num; hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; @@ -683,11 +603,7 @@ static hal_error_t pkey_remote_get_key_type(const hal_pkey_handle_t pkey, check(hal_xdr_encode_int(&optr, olimit, pkey.handle)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_PKEY_GET_KEY_TYPE, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); if (rpc_ret == HAL_OK) { @@ -703,9 +619,7 @@ static hal_error_t pkey_remote_get_key_flags(const hal_pkey_handle_t pkey, uint8_t outbuf[nargs(3)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(4)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); uint32_t flags32; - uint32_t rpc_func_num; hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; @@ -714,11 +628,7 @@ static hal_error_t pkey_remote_get_key_flags(const hal_pkey_handle_t pkey, check(hal_xdr_encode_int(&optr, olimit, pkey.handle)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_PKEY_GET_KEY_FLAGS, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); if (rpc_ret == HAL_OK) { @@ -733,9 +643,7 @@ static size_t pkey_remote_get_public_key_len(const hal_pkey_handle_t pkey) uint8_t outbuf[nargs(3)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(4)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); uint32_t len32; - uint32_t rpc_func_num; hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; @@ -744,11 +652,7 @@ static size_t pkey_remote_get_public_key_len(const hal_pkey_handle_t pkey) check(hal_xdr_encode_int(&optr, olimit, pkey.handle)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_PKEY_GET_PUBLIC_KEY_LEN, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); if (rpc_ret == HAL_OK) { @@ -765,9 +669,7 @@ static hal_error_t pkey_remote_get_public_key(const hal_pkey_handle_t pkey, uint8_t outbuf[nargs(4)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(4) + pad(der_max)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); uint32_t dlen32 = der_max; - uint32_t rpc_func_num; hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; @@ -777,11 +679,7 @@ static hal_error_t pkey_remote_get_public_key(const hal_pkey_handle_t pkey, check(hal_xdr_encode_int(&optr, olimit, der_max)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_PKEY_GET_PUBLIC_KEY, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); if (rpc_ret == HAL_OK) { @@ -800,13 +698,11 @@ static hal_error_t pkey_remote_sign(const hal_session_handle_t session, uint8_t outbuf[nargs(7) + pad(input_len)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(4) + pad(signature_max)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); uint32_t slen32 = signature_max; - uint32_t rpc_func_num; hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; - check(hal_xdr_encode_int(&optr, olimit, RPC_FUNC_PKEY_REMOTE_SIGN)); + check(hal_xdr_encode_int(&optr, olimit, RPC_FUNC_PKEY_SIGN)); check(hal_xdr_encode_int(&optr, olimit, dummy_client.handle)); check(hal_xdr_encode_int(&optr, olimit, session.handle)); check(hal_xdr_encode_int(&optr, olimit, pkey.handle)); @@ -815,11 +711,7 @@ static hal_error_t pkey_remote_sign(const hal_session_handle_t session, check(hal_xdr_encode_int(&optr, olimit, signature_max)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_PKEY_SIGN, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); if (rpc_ret == HAL_OK) { @@ -838,12 +730,10 @@ static hal_error_t pkey_remote_verify(const hal_session_handle_t session, uint8_t outbuf[nargs(7) + pad(input_len) + pad(signature_len)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(3)]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); - uint32_t rpc_func_num; hal_client_handle_t dummy_client = {0}; hal_error_t rpc_ret; - check(hal_xdr_encode_int(&optr, olimit, RPC_FUNC_PKEY_REMOTE_VERIFY)); + check(hal_xdr_encode_int(&optr, olimit, RPC_FUNC_PKEY_VERIFY)); check(hal_xdr_encode_int(&optr, olimit, dummy_client.handle)); check(hal_xdr_encode_int(&optr, olimit, session.handle)); check(hal_xdr_encode_int(&optr, olimit, pkey.handle)); @@ -852,11 +742,7 @@ static hal_error_t pkey_remote_verify(const hal_session_handle_t session, check(hal_xdr_encode_buffer(&optr, olimit, signature, signature_len)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_PKEY_VERIFY, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); return rpc_ret; @@ -881,9 +767,7 @@ static hal_error_t pkey_remote_list(hal_pkey_info_t *result, uint8_t outbuf[nargs(4)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf); uint8_t inbuf[nargs(4) + pad(result_max * sizeof(hal_pkey_info_t))]; const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf); - size_t ilen = sizeof(inbuf); uint32_t len; - uint32_t rpc_func_num; hal_client_handle_t dummy_client = {0}; hal_error_t ret, rpc_ret; @@ -893,11 +777,7 @@ static hal_error_t pkey_remote_list(hal_pkey_info_t *result, check(hal_xdr_encode_int(&optr, olimit, flags)); check(hal_rpc_send(outbuf, optr - outbuf)); - check(hal_rpc_recv(inbuf, &ilen)); - assert(ilen <= sizeof(inbuf)); - - check(hal_xdr_decode_int(&iptr, ilimit, &rpc_func_num)); - check(hal_xdr_decode_int(&iptr, ilimit, &dummy_client.handle)); + check(read_matching_packet(RPC_FUNC_PKEY_LIST, inbuf, sizeof(inbuf), &iptr, &ilimit)); check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret)); if (rpc_ret == HAL_OK) { diff --git a/rpc_server.c b/rpc_server.c index 7b06780..ff6367c 100644 --- a/rpc_server.c +++ b/rpc_server.c @@ -731,10 +731,10 @@ void hal_rpc_server_dispatch(const uint8_t * const ibuf, const size_t ilen, case RPC_FUNC_PKEY_GET_PUBLIC_KEY: ret = pkey_get_public_key(&iptr, ilimit, &optr, olimit); break; - case RPC_FUNC_PKEY_REMOTE_SIGN: + case RPC_FUNC_PKEY_SIGN: ret = pkey_remote_sign(&iptr, ilimit, &optr, olimit); break; - case RPC_FUNC_PKEY_REMOTE_VERIFY: + case RPC_FUNC_PKEY_VERIFY: ret = pkey_remote_verify(&iptr, ilimit, &optr, olimit); break; case RPC_FUNC_PKEY_LIST: |