aboutsummaryrefslogtreecommitdiff
path: root/rpc_client.c
diff options
context:
space:
mode:
authorPaul Selkirk <paul@psgd.org>2018-02-27 17:54:06 +0100
committerPaul Selkirk <paul@psgd.org>2018-02-27 17:54:06 +0100
commit067e04123c15558e84d77f822e704f664292f740 (patch)
tree1cd3e11d288f1f8ef01ba8f30ac10da9fbd8c2aa /rpc_client.c
parent8529d05262aa636a1bb701e19a34c371d49a6fe4 (diff)
Clean up RPC code, track changes to XDR API.
Diffstat (limited to 'rpc_client.c')
-rw-r--r--rpc_client.c103
1 files changed, 47 insertions, 56 deletions
diff --git a/rpc_client.c b/rpc_client.c
index e968369..bb63448 100644
--- a/rpc_client.c
+++ b/rpc_client.c
@@ -4,7 +4,7 @@
* Remote procedure call client-side private API implementation.
*
* Authors: Rob Austein, Paul Selkirk
- * Copyright (c) 2015-2016, NORDUnet A/S All rights reserved.
+ * Copyright (c) 2015-2018, 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
@@ -139,7 +139,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);
- uint32_t rcvlen = length;
+ size_t rcvlen = length;
hal_client_handle_t dummy_client = {0};
hal_error_t rpc_ret;
@@ -152,7 +152,7 @@ static hal_error_t get_random(void *buffer, const size_t length)
check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret));
if (rpc_ret == HAL_OK) {
- check(hal_xdr_decode_buffer(&iptr, ilimit, buffer, &rcvlen));
+ check(hal_xdr_decode_variable_opaque(&iptr, ilimit, buffer, &rcvlen));
// XXX check rcvlen vs length
}
return rpc_ret;
@@ -170,7 +170,7 @@ static hal_error_t set_pin(const hal_client_handle_t client,
check(hal_xdr_encode_int(&optr, olimit, RPC_FUNC_SET_PIN));
check(hal_xdr_encode_int(&optr, olimit, client.handle));
check(hal_xdr_encode_int(&optr, olimit, user));
- check(hal_xdr_encode_buffer(&optr, olimit, (const uint8_t *)pin, pin_len));
+ check(hal_xdr_encode_variable_opaque(&optr, olimit, (const uint8_t *)pin, pin_len));
check(hal_rpc_send(outbuf, optr - outbuf));
check(read_matching_packet(RPC_FUNC_SET_PIN, inbuf, sizeof(inbuf), &iptr, &ilimit));
@@ -205,7 +205,7 @@ static hal_error_t login(const hal_client_handle_t client,
check(hal_xdr_encode_int(&optr, olimit, RPC_FUNC_LOGIN));
check(hal_xdr_encode_int(&optr, olimit, client.handle));
check(hal_xdr_encode_int(&optr, olimit, user));
- check(hal_xdr_encode_buffer(&optr, olimit, (const uint8_t *)pin, pin_len));
+ check(hal_xdr_encode_variable_opaque(&optr, olimit, (const uint8_t *)pin, pin_len));
check(hal_rpc_send(outbuf, optr - outbuf));
check(read_matching_packet(RPC_FUNC_LOGIN, inbuf, sizeof(inbuf), &iptr, &ilimit));
@@ -298,7 +298,6 @@ 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);
- uint32_t len32 = len_max;
hal_client_handle_t dummy_client = {0};
hal_error_t rpc_ret;
@@ -313,8 +312,8 @@ static hal_error_t hash_get_digest_algorithm_id(const hal_digest_algorithm_t alg
check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret));
if (rpc_ret == HAL_OK) {
- check(hal_xdr_decode_buffer(&iptr, ilimit, id, &len32));
- *len = len32;
+ *len = len_max;
+ check(hal_xdr_decode_variable_opaque(&iptr, ilimit, id, len));
}
return rpc_ret;
}
@@ -358,7 +357,7 @@ static hal_error_t hash_initialize(const hal_client_handle_t client,
check(hal_xdr_encode_int(&optr, olimit, client.handle));
check(hal_xdr_encode_int(&optr, olimit, session.handle));
check(hal_xdr_encode_int(&optr, olimit, alg));
- check(hal_xdr_encode_buffer(&optr, olimit, key, key_len));
+ check(hal_xdr_encode_variable_opaque(&optr, olimit, key, key_len));
check(hal_rpc_send(outbuf, optr - outbuf));
check(read_matching_packet(RPC_FUNC_HASH_INITIALIZE, inbuf, sizeof(inbuf), &iptr, &ilimit));
@@ -382,7 +381,7 @@ static hal_error_t hash_update(const hal_hash_handle_t hash,
check(hal_xdr_encode_int(&optr, olimit, RPC_FUNC_HASH_UPDATE));
check(hal_xdr_encode_int(&optr, olimit, dummy_client.handle));
check(hal_xdr_encode_int(&optr, olimit, hash.handle));
- check(hal_xdr_encode_buffer(&optr, olimit, data, length));
+ check(hal_xdr_encode_variable_opaque(&optr, olimit, data, length));
check(hal_rpc_send(outbuf, optr - outbuf));
check(read_matching_packet(RPC_FUNC_HASH_UPDATE, inbuf, sizeof(inbuf), &iptr, &ilimit));
@@ -397,7 +396,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);
- uint32_t digest_len = length;
+ size_t digest_len = length;
hal_client_handle_t dummy_client = {0};
hal_error_t rpc_ret;
@@ -411,7 +410,7 @@ static hal_error_t hash_finalize(const hal_hash_handle_t hash,
check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret));
if (rpc_ret == HAL_OK) {
- check(hal_xdr_decode_buffer(&iptr, ilimit, digest, &digest_len));
+ check(hal_xdr_decode_variable_opaque(&iptr, ilimit, digest, &digest_len));
/* XXX check digest_len vs length */
}
return rpc_ret;
@@ -427,27 +426,25 @@ static hal_error_t pkey_remote_load(const hal_client_handle_t client,
uint8_t outbuf[nargs(5) + pad(der_len)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf);
uint8_t inbuf[nargs(5) + pad(sizeof(name->uuid))];
const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf);
- uint32_t name_len = sizeof(name->uuid);
+ size_t name_len = sizeof(name->uuid);
hal_error_t rpc_ret;
check(hal_xdr_encode_int(&optr, olimit, RPC_FUNC_PKEY_LOAD));
check(hal_xdr_encode_int(&optr, olimit, client.handle));
check(hal_xdr_encode_int(&optr, olimit, session.handle));
- check(hal_xdr_encode_buffer(&optr, olimit, der, der_len));
+ check(hal_xdr_encode_variable_opaque(&optr, olimit, der, der_len));
check(hal_xdr_encode_int(&optr, olimit, flags));
check(hal_rpc_send(outbuf, optr - outbuf));
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) {
check(hal_xdr_decode_int(&iptr, ilimit, &pkey->handle));
- check(hal_xdr_decode_buffer(&iptr, ilimit, name->uuid, &name_len));
+ check(hal_xdr_decode_variable_opaque(&iptr, ilimit, name->uuid, &name_len));
if (name_len != sizeof(name->uuid))
return HAL_ERROR_KEY_NAME_TOO_LONG;
}
-
return rpc_ret;
}
@@ -464,7 +461,7 @@ static hal_error_t pkey_remote_open(const hal_client_handle_t client,
check(hal_xdr_encode_int(&optr, olimit, RPC_FUNC_PKEY_OPEN));
check(hal_xdr_encode_int(&optr, olimit, client.handle));
check(hal_xdr_encode_int(&optr, olimit, session.handle));
- check(hal_xdr_encode_buffer(&optr, olimit, name->uuid, sizeof(name->uuid)));
+ check(hal_xdr_encode_variable_opaque(&optr, olimit, name->uuid, sizeof(name->uuid)));
check(hal_rpc_send(outbuf, optr - outbuf));
check(read_matching_packet(RPC_FUNC_PKEY_OPEN, inbuf, sizeof(inbuf), &iptr, &ilimit));
@@ -488,14 +485,14 @@ static hal_error_t pkey_remote_generate_rsa(const hal_client_handle_t client,
uint8_t outbuf[nargs(6) + pad(exp_len)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf);
uint8_t inbuf[nargs(5) + pad(sizeof(name->uuid))];
const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf);
- uint32_t name_len = sizeof(name->uuid);
+ size_t name_len = sizeof(name->uuid);
hal_error_t rpc_ret;
check(hal_xdr_encode_int(&optr, olimit, RPC_FUNC_PKEY_GENERATE_RSA));
check(hal_xdr_encode_int(&optr, olimit, client.handle));
check(hal_xdr_encode_int(&optr, olimit, session.handle));
check(hal_xdr_encode_int(&optr, olimit, key_len));
- check(hal_xdr_encode_buffer(&optr, olimit, exp, exp_len));
+ check(hal_xdr_encode_variable_opaque(&optr, olimit, exp, exp_len));
check(hal_xdr_encode_int(&optr, olimit, flags));
check(hal_rpc_send(outbuf, optr - outbuf));
@@ -505,7 +502,7 @@ static hal_error_t pkey_remote_generate_rsa(const hal_client_handle_t client,
if (rpc_ret == HAL_OK) {
check(hal_xdr_decode_int(&iptr, ilimit, &pkey->handle));
- check(hal_xdr_decode_buffer(&iptr, ilimit, name->uuid, &name_len));
+ check(hal_xdr_decode_variable_opaque(&iptr, ilimit, name->uuid, &name_len));
if (name_len != sizeof(name->uuid))
return HAL_ERROR_KEY_NAME_TOO_LONG;
}
@@ -523,7 +520,7 @@ static hal_error_t pkey_remote_generate_ec(const hal_client_handle_t client,
uint8_t outbuf[nargs(5)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf);
uint8_t inbuf[nargs(5) + pad(sizeof(name->uuid))];
const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf);
- uint32_t name_len = sizeof(name->uuid);
+ size_t name_len = sizeof(name->uuid);
hal_error_t rpc_ret;
check(hal_xdr_encode_int(&optr, olimit, RPC_FUNC_PKEY_GENERATE_EC));
@@ -539,7 +536,7 @@ static hal_error_t pkey_remote_generate_ec(const hal_client_handle_t client,
if (rpc_ret == HAL_OK) {
check(hal_xdr_decode_int(&iptr, ilimit, &pkey->handle));
- check(hal_xdr_decode_buffer(&iptr, ilimit, name->uuid, &name_len));
+ check(hal_xdr_decode_variable_opaque(&iptr, ilimit, name->uuid, &name_len));
if (name_len != sizeof(name->uuid))
return HAL_ERROR_KEY_NAME_TOO_LONG;
}
@@ -677,12 +674,11 @@ static size_t pkey_remote_get_public_key_len(const hal_pkey_handle_t pkey)
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) {
- check(hal_xdr_decode_int(&iptr, ilimit, &len32));
+ if (rpc_ret == HAL_OK &&
+ hal_xdr_decode_int(&iptr, ilimit, &len32) == HAL_OK)
return (size_t)len32;
- }
- else
- return 0;
+
+ return 0;
}
static hal_error_t pkey_remote_get_public_key(const hal_pkey_handle_t pkey,
@@ -691,7 +687,6 @@ 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);
- uint32_t dlen32 = der_max;
hal_client_handle_t dummy_client = {0};
hal_error_t rpc_ret;
@@ -705,8 +700,8 @@ static hal_error_t pkey_remote_get_public_key(const hal_pkey_handle_t pkey,
check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret));
if (rpc_ret == HAL_OK) {
- check(hal_xdr_decode_buffer(&iptr, ilimit, der, &dlen32));
- *der_len = (size_t)dlen32;
+ *der_len = der_max;
+ check(hal_xdr_decode_variable_opaque(&iptr, ilimit, der, der_len));
}
return rpc_ret;
}
@@ -719,7 +714,6 @@ static hal_error_t pkey_remote_sign(const hal_pkey_handle_t pkey,
uint8_t outbuf[nargs(6) + 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);
- uint32_t slen32 = signature_max;
hal_client_handle_t dummy_client = {0};
hal_error_t rpc_ret;
@@ -727,7 +721,7 @@ static hal_error_t pkey_remote_sign(const hal_pkey_handle_t pkey,
check(hal_xdr_encode_int(&optr, olimit, dummy_client.handle));
check(hal_xdr_encode_int(&optr, olimit, pkey.handle));
check(hal_xdr_encode_int(&optr, olimit, hash.handle));
- check(hal_xdr_encode_buffer(&optr, olimit, input, input_len));
+ check(hal_xdr_encode_variable_opaque(&optr, olimit, input, input_len));
check(hal_xdr_encode_int(&optr, olimit, signature_max));
check(hal_rpc_send(outbuf, optr - outbuf));
@@ -735,8 +729,8 @@ static hal_error_t pkey_remote_sign(const hal_pkey_handle_t pkey,
check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret));
if (rpc_ret == HAL_OK) {
- check(hal_xdr_decode_buffer(&iptr, ilimit, signature, &slen32));
- *signature_len = (size_t)slen32;
+ *signature_len = signature_max;
+ check(hal_xdr_decode_variable_opaque(&iptr, ilimit, signature, signature_len));
}
return rpc_ret;
}
@@ -756,8 +750,8 @@ static hal_error_t pkey_remote_verify(const hal_pkey_handle_t pkey,
check(hal_xdr_encode_int(&optr, olimit, dummy_client.handle));
check(hal_xdr_encode_int(&optr, olimit, pkey.handle));
check(hal_xdr_encode_int(&optr, olimit, hash.handle));
- check(hal_xdr_encode_buffer(&optr, olimit, input, input_len));
- check(hal_xdr_encode_buffer(&optr, olimit, signature, signature_len));
+ check(hal_xdr_encode_variable_opaque(&optr, olimit, input, input_len));
+ check(hal_xdr_encode_variable_opaque(&optr, olimit, signature, signature_len));
check(hal_rpc_send(outbuf, optr - outbuf));
check(read_matching_packet(RPC_FUNC_PKEY_VERIFY, inbuf, sizeof(inbuf), &iptr, &ilimit));
@@ -802,12 +796,12 @@ static hal_error_t pkey_remote_match(const hal_client_handle_t client,
if (attributes != NULL) {
for (int i = 0; i < attributes_len; i++) {
check(hal_xdr_encode_int(&optr, olimit, attributes[i].type));
- check(hal_xdr_encode_buffer(&optr, olimit, attributes[i].value, attributes[i].length));
+ check(hal_xdr_encode_variable_opaque(&optr, olimit, attributes[i].value, attributes[i].length));
}
}
check(hal_xdr_encode_int(&optr, olimit, *state));
check(hal_xdr_encode_int(&optr, olimit, result_max));
- check(hal_xdr_encode_buffer(&optr, olimit, previous_uuid->uuid, sizeof(previous_uuid->uuid)));
+ check(hal_xdr_encode_variable_opaque(&optr, olimit, previous_uuid->uuid, sizeof(previous_uuid->uuid)));
check(hal_rpc_send(outbuf, optr - outbuf));
check(read_matching_packet(RPC_FUNC_PKEY_MATCH, inbuf, sizeof(inbuf), &iptr, &ilimit));
@@ -820,8 +814,8 @@ static hal_error_t pkey_remote_match(const hal_client_handle_t client,
*state = ustate;
check(hal_xdr_decode_int(&iptr, ilimit, &array_len));
for (int i = 0; i < array_len; ++i) {
- uint32_t uuid_len = sizeof(result[i].uuid);
- check(hal_xdr_decode_buffer(&iptr, ilimit, result[i].uuid, &uuid_len));
+ size_t uuid_len = sizeof(result[i].uuid);
+ check(hal_xdr_decode_variable_opaque(&iptr, ilimit, result[i].uuid, &uuid_len));
if (uuid_len != sizeof(result[i].uuid))
return HAL_ERROR_KEY_NAME_TOO_LONG;
}
@@ -853,7 +847,7 @@ static hal_error_t pkey_remote_set_attributes(const hal_pkey_handle_t pkey,
if (attributes[i].length == HAL_PKEY_ATTRIBUTE_NIL)
check(hal_xdr_encode_int(&optr, olimit, HAL_PKEY_ATTRIBUTE_NIL));
else
- check(hal_xdr_encode_buffer(&optr, olimit, attributes[i].value, attributes[i].length));
+ check(hal_xdr_encode_variable_opaque(&optr, olimit, attributes[i].value, attributes[i].length));
}
check(hal_rpc_send(outbuf, optr - outbuf));
@@ -904,10 +898,10 @@ static hal_error_t pkey_remote_get_attributes(const hal_pkey_handle_t pkey,
attributes[i].length = u32;
}
else {
- u32 = attributes_buffer + attributes_buffer_len - abuf;
- check(hal_xdr_decode_buffer(&iptr, ilimit, abuf, &u32));
+ size_t len = attributes_buffer + attributes_buffer_len - abuf;
+ check(hal_xdr_decode_variable_opaque(&iptr, ilimit, abuf, &len));
attributes[i].value = abuf;
- attributes[i].length = u32;
+ attributes[i].length = len;
abuf += u32;
}
}
@@ -938,13 +932,10 @@ static hal_error_t pkey_remote_export(const hal_pkey_handle_t pkey,
check(hal_xdr_decode_int(&iptr, ilimit, &rpc_ret));
if (rpc_ret == HAL_OK) {
- uint32_t len;
- len = pkcs8_max;
- check(hal_xdr_decode_buffer(&iptr, ilimit, pkcs8, &len));
- *pkcs8_len = (size_t) len;
- len = kek_max;
- check(hal_xdr_decode_buffer(&iptr, ilimit, kek, &len));
- *kek_len = (size_t) len;
+ *pkcs8_len = pkcs8_max;
+ check(hal_xdr_decode_variable_opaque(&iptr, ilimit, pkcs8, pkcs8_len));
+ *kek_len = kek_max;
+ check(hal_xdr_decode_variable_opaque(&iptr, ilimit, kek, kek_len));
}
return rpc_ret;
}
@@ -961,15 +952,15 @@ static hal_error_t pkey_remote_import(const hal_client_handle_t client,
uint8_t outbuf[nargs(7) + pad(pkcs8_len) + pad(kek_len)], *optr = outbuf, *olimit = outbuf + sizeof(outbuf);
uint8_t inbuf[nargs(5) + pad(sizeof(name->uuid))];
const uint8_t *iptr = inbuf, *ilimit = inbuf + sizeof(inbuf);
- uint32_t name_len = sizeof(name->uuid);
+ size_t name_len = sizeof(name->uuid);
hal_error_t rpc_ret;
check(hal_xdr_encode_int(&optr, olimit, RPC_FUNC_PKEY_IMPORT));
check(hal_xdr_encode_int(&optr, olimit, client.handle));
check(hal_xdr_encode_int(&optr, olimit, session.handle));
check(hal_xdr_encode_int(&optr, olimit, kekek.handle));
- check(hal_xdr_encode_buffer(&optr, olimit, pkcs8, pkcs8_len));
- check(hal_xdr_encode_buffer(&optr, olimit, kek, kek_len));
+ check(hal_xdr_encode_variable_opaque(&optr, olimit, pkcs8, pkcs8_len));
+ check(hal_xdr_encode_variable_opaque(&optr, olimit, kek, kek_len));
check(hal_xdr_encode_int(&optr, olimit, flags));
check(hal_rpc_send(outbuf, optr - outbuf));
@@ -979,7 +970,7 @@ static hal_error_t pkey_remote_import(const hal_client_handle_t client,
if (rpc_ret == HAL_OK) {
check(hal_xdr_decode_int(&iptr, ilimit, &pkey->handle));
- check(hal_xdr_decode_buffer(&iptr, ilimit, name->uuid, &name_len));
+ check(hal_xdr_decode_variable_opaque(&iptr, ilimit, name->uuid, &name_len));
if (name_len != sizeof(name->uuid))
return HAL_ERROR_KEY_NAME_TOO_LONG;
}