Skip to content

Commit

Permalink
fix mspid and channelid value once set
Browse files Browse the repository at this point in the history
Signed-off-by: Bruno Vavala <bruno.vavala@intel.com>
  • Loading branch information
bvavala committed Jul 20, 2020
1 parent 4360807 commit 23347ab
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 20 deletions.
1 change: 1 addition & 0 deletions ecc_enclave/enclave/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ set(SOURCE_FILES
enclave.cpp
enclave_t.c
shim.cpp
shim_internals.cpp
${COMMON_SOURCE_DIR}/enclave/common.cpp
${COMMON_SOURCE_DIR}/base64/base64.cpp
${COMMON_SOURCE_DIR}/utils.c
Expand Down
16 changes: 4 additions & 12 deletions ecc_enclave/enclave/shim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,14 @@ void get_creator_name(

void get_channel_id(char* channel_id, uint32_t max_channel_id_len, shim_ctx_ptr_t ctx)
{
if (max_channel_id_len > 0)
{
channel_id[0] = '\0';
ocall_get_channel_id(channel_id, max_channel_id_len, ctx->u_shim_ctx);
channel_id[max_channel_id_len - 1] = '\0';
}
memset(channel_id, 0, max_channel_id_len);
internal_get_channel_id(channel_id, max_channel_id_len, ctx);
}

void get_msp_id(char* msp_id, uint32_t max_msp_id_len, shim_ctx_ptr_t ctx)
{
if (max_msp_id_len > 0)
{
msp_id[0] = '\0';
ocall_get_msp_id(msp_id, max_msp_id_len, ctx->u_shim_ctx);
msp_id[max_msp_id_len - 1] = '\0';
}
memset(msp_id, 0, max_msp_id_len);
internal_get_msp_id(msp_id, max_msp_id_len, ctx);
}

void get_state(
Expand Down
8 changes: 0 additions & 8 deletions ecc_enclave/enclave/shim.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,6 @@ int get_func_and_params(
// transaction APIs
//-------------------------------------------------

// - getChannelID
// // TOD0 (possible extensions): might be useful to support and should be easy?
// // If this is just the name, would it be useful also to have a variant which
// // has the unique id ("content-addressable"/genesis-block-hash)?
// void get_channel_id(char* channel_id,
// uint32_t max_channel_id_len,
// shim_ctx_ptr_t ctx);

// - TxID
// // TODO (possible extensions): at least coming from a Sawtooth/PDO perspective,
// // i would think access to this info might be important for cross-cc transactions?
Expand Down
133 changes: 133 additions & 0 deletions ecc_enclave/enclave/shim_internals.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright 2020 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "shim_internals.h"
#include "enclave_t.h" // for ocalls
#include "logging.h" // for LOG_*

/******************************************************************************
* The channel id is set once, and then maintained fixed for consistency.
* The value is meant to be either provided by the Fabric shim on enclave
* creation, or set after unsealing the chaincode parameters.
* In both cases, the value is not verified. The verification is performed
* by the Enclave Registry when the chaincode/enclave parameters are registered.
*****************************************************************************/

static char g_channel_id[MAX_CHANNEL_ID_LENGTH];
static uint32_t g_channel_id_length;
static bool g_channel_id_set = false;

bool internal_set_channel_id(char* channel_id, uint32_t channel_id_length)
{
if (g_channel_id_set)
{
LOG_ERROR("channel id already set");
return false;
}

if (channel_id_length + 1 > MAX_CHANNEL_ID_LENGTH)
{
LOG_ERROR("channel id %s (length %u) too long", channel_id_length);
return false;
}

strncpy(g_channel_id, channel_id, channel_id_length);
g_channel_id_length = channel_id_length;
g_channel_id[g_channel_id_length + 1] = '\0';
g_channel_id_set = true;
return true;
}

bool internal_get_channel_id(char* channel_id, uint32_t max_channel_id_len, shim_ctx_ptr_t ctx)
{
if (!g_channel_id_set)
{
// get channel id
char local_channel_id[MAX_CHANNEL_ID_LENGTH];
ocall_get_channel_id(local_channel_id, MAX_CHANNEL_ID_LENGTH, ctx->u_shim_ctx);
// make sure string is null terminated
local_channel_id[MAX_CHANNEL_ID_LENGTH - 1] = '\0';
// set internal channel id
if (!internal_set_channel_id(local_channel_id, strlen(local_channel_id)))
{
return false;
}
}

// channel id is set

if (max_channel_id_len < g_channel_id_length + 1)
{
LOG_ERROR("input channel id buffer length is insufficient");
return false;
}

strncpy(channel_id, g_channel_id, g_channel_id_length);
channel_id[g_channel_id_length + 1] = '\0';
return true;
}

/******************************************************************************
* The msp id is set once, and then maintained fixed for consistency.
* The value is meant to be either provided by the Fabric shim on enclave
* creation, or set after unsealing the chaincode parameters.
* In both cases, the value is not verified. The verification is performed
* by the Enclave Registry when the chaincode/enclave parameters are registered.
*****************************************************************************/

static char g_msp_id[MAX_MSP_ID_LENGTH];
static uint32_t g_msp_id_length;
static bool g_msp_id_set = false;

bool internal_set_msp_id(char* msp_id, uint32_t msp_id_length)
{
if (g_msp_id_set)
{
LOG_ERROR("msp id already set");
return false;
}

if (msp_id_length + 1 > MAX_CHANNEL_ID_LENGTH)
{
LOG_ERROR("msp id %s (length %u) too long", msp_id_length);
return false;
}

strncpy(g_msp_id, msp_id, msp_id_length);
g_msp_id_length = msp_id_length;
g_msp_id[g_msp_id_length + 1] = '\0';
g_msp_id_set = true;
return true;
}

bool internal_get_msp_id(char* msp_id, uint32_t max_msp_id_len, shim_ctx_ptr_t ctx)
{
if (!g_msp_id_set)
{
// get msp id
char local_msp_id[MAX_CHANNEL_ID_LENGTH];
ocall_get_msp_id(local_msp_id, MAX_CHANNEL_ID_LENGTH, ctx->u_shim_ctx);
// make sure string is null terminated
local_msp_id[MAX_CHANNEL_ID_LENGTH - 1] = '\0';
// set internal msp id
if (!internal_set_msp_id(local_msp_id, strlen(local_msp_id)))
{
return false;
}
}

// msp id is set

if (max_msp_id_len < g_msp_id_length + 1)
{
LOG_ERROR("input msp id buffer length is insufficient");
return false;
}

strncpy(msp_id, g_msp_id, g_msp_id_length);
msp_id[g_msp_id_length + 1] = '\0';
return true;
}
10 changes: 10 additions & 0 deletions ecc_enclave/enclave/shim_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#include <map>
#include <set>
#include <string>
#include "shim.h" // for shim_ctx_ptr_t

#define MAX_CHANNEL_ID_LENGTH 1024
#define MAX_MSP_ID_LENGTH 1024

// read/writeset
typedef std::map<std::string, std::string> write_set_t;
Expand All @@ -24,3 +28,9 @@ typedef struct t_shim_ctx
const char* encoded_args; // args as passed from client-side shim, potentially encrypted
const char* json_args; // clear-text args from client-side shim
} t_shim_ctx_t;

bool internal_set_channel_id(char* channel_id, uint32_t channel_id_length);
bool internal_get_channel_id(char* channel_id, uint32_t max_channel_id_len, shim_ctx_ptr_t ctx);

bool internal_set_msp_id(char* msp_id, uint32_t msp_id_length);
bool internal_get_msp_id(char* msp_id, uint32_t max_msp_id_len, shim_ctx_ptr_t ctx);

0 comments on commit 23347ab

Please sign in to comment.