From 77b70b136cce2f58d89db4e3d2edb6d41d8a023c Mon Sep 17 00:00:00 2001 From: Oleksii Beketov Date: Thu, 19 Jul 2018 16:38:43 +0300 Subject: [PATCH] RPK API update Callback for RPK master key declaration corrected https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/pull/301 (cherry picked from commit da0a7f68cb47d41a0496161ce46d9a3f3b6a74d0) Change-Id: I2f0f92f6068eb69801c46201394fd703010b2b64 Signed-off-by: Oleksii Beketov Signed-off-by: DoHyun Pyun --- resource/csdk/security/include/pinoxmcommon.h | 14 ++----- .../provisioning/include/oxm/oxmrawpublickey.h | 13 +++++++ .../provisioning/sample/provisioningclient.c | 21 ++++++----- .../security/provisioning/src/oxmrawpublickey.c | 43 ++++++++++++---------- resource/csdk/security/src/oxmpincommon.c | 4 +- 5 files changed, 53 insertions(+), 42 deletions(-) diff --git a/resource/csdk/security/include/pinoxmcommon.h b/resource/csdk/security/include/pinoxmcommon.h index c23b24c..aa40dd5 100644 --- a/resource/csdk/security/include/pinoxmcommon.h +++ b/resource/csdk/security/include/pinoxmcommon.h @@ -59,10 +59,11 @@ typedef void (*GeneratePinCallback)(char* pinData, size_t pinSize); typedef void (*InputPinCallback)(char* pinBuf, size_t bufSize); /** - * Function pointer for getting peer's public key, own public and private keys and token - * for raw public key generation. + * Function pointer for getting master key for raw public key OTM. + * Callback is expected to set *rpkMasterKey pointer to binary data buffer + * containing the key. Master key lenght must not exceed OXM_RPK_MASTER_KEY_MAX_SIZE. */ -typedef void (*GetRPKMasterKeyCallback)(char *rpkMasterKey, size_t *rpkMasterKeyLen); +typedef void (*GetRPKMasterKeyCallback)(char **rpkMasterKey, size_t *rpkMasterKeyLen); /** * Function pointer to close the displied PIN. @@ -84,13 +85,6 @@ void SetGeneratePinCB(GeneratePinCallback pinCB); void SetInputPinCB(InputPinCallback pinCB); /** - * Function to setting RPK key callback from user. - * - * @param rpkCB implementation of RPK parameters callback. - */ -void SetGetRPKMasterKeyCB(GetRPKMasterKeyCallback rpkCB); - -/** * Function to set the close PIN callback * This callback will be invoked when PIN based OTM is finished. * diff --git a/resource/csdk/security/provisioning/include/oxm/oxmrawpublickey.h b/resource/csdk/security/provisioning/include/oxm/oxmrawpublickey.h index 56513a2..b6d50ec 100644 --- a/resource/csdk/security/provisioning/include/oxm/oxmrawpublickey.h +++ b/resource/csdk/security/provisioning/include/oxm/oxmrawpublickey.h @@ -43,6 +43,19 @@ extern "C" { OCStackResult GetMasterRPKCallback(OTMContext_t *otmCtx); /** + * Function to setting RPK key callback from user. + * + * @param rpkCB implementation of RPK parameters callback. + */ +OCStackResult SetRPKMasterKeyCB(GetRPKMasterKeyCallback rpkCB); + +/** + * Function to unset the RPK master key callback. + * NOTE : Do not call this function while RPK based ownership transfer. + */ +OCStackResult UnsetRPKMasterKeyCB(); + +/** * Callback implemenration to establish a secure channel with PSK cipher suite. * * @param otmCtx Context of OTM, It includes current device information. diff --git a/resource/csdk/security/provisioning/sample/provisioningclient.c b/resource/csdk/security/provisioning/sample/provisioningclient.c index af45c6f..301a625 100644 --- a/resource/csdk/security/provisioning/sample/provisioningclient.c +++ b/resource/csdk/security/provisioning/sample/provisioningclient.c @@ -37,6 +37,7 @@ #include "srmutility.h" #include "pmtypes.h" #include "oxmverifycommon.h" +#include "oxmrawpublickey.h" #include "pkix_interface.h" #include "hw_emul/hw_interface.h" #include "mbedtls/x509_crt.h" @@ -313,7 +314,14 @@ static void inputPinCB(char* pin, size_t len) } } -static void InputRPKMasterKeyCB(uint8_t *rpkMasterKey, int *rpkMasterKeyLen) +static char rpk[32] = { + 0xB2, 0xA2, 0x0D, 0xC0, 0xCB, 0x3C, 0xA0, 0x27, + 0x45, 0x00, 0x73, 0xBD, 0x02, 0xF5, 0x84, 0x4B, + 0x0C, 0x1C, 0xD6, 0x6A, 0xD3, 0x9E, 0x3F, 0x64, + 0x95, 0x6B, 0xB8, 0xCA, 0x58, 0xBC, 0xBE, 0xBE +}; + +static void InputRPKMasterKeyCB(char **rpkMasterKey, size_t *rpkMasterKeyLen) { if(!rpkMasterKey || !rpkMasterKeyLen) { @@ -321,14 +329,7 @@ static void InputRPKMasterKeyCB(uint8_t *rpkMasterKey, int *rpkMasterKeyLen) return; } - uint8_t rpk[32] = { - 0xfd, 0xb5, 0x48, 0xec, 0x9c, 0x16, 0xc9, 0x72, - 0xc6, 0xee, 0xaf, 0x42, 0x64, 0x0d, 0xe5, 0xb6, - 0x44, 0x78, 0x84, 0x9e, 0xdd, 0x5c, 0x1d, 0x31, - 0xcf, 0xd0, 0xf2, 0xe8, 0xb1, 0xa4, 0xad, 0x0f - }; - - memcpy(rpkMasterKey, rpk, 32); + *rpkMasterKey = rpk; *rpkMasterKeyLen = 32; } @@ -377,7 +378,7 @@ static int initProvisionClient(void) } SetInputPinCB(inputPinCB); - SetInputRPKMasterKeyCB(InputRPKMasterKeyCB); + SetRPKMasterKeyCB(InputRPKMasterKeyCB); return 0; } diff --git a/resource/csdk/security/provisioning/src/oxmrawpublickey.c b/resource/csdk/security/provisioning/src/oxmrawpublickey.c index 62a3be3..cda2011 100644 --- a/resource/csdk/security/provisioning/src/oxmrawpublickey.c +++ b/resource/csdk/security/provisioning/src/oxmrawpublickey.c @@ -53,20 +53,24 @@ static RPKOxmData_t g_RPKOxmData = { static GetRPKMasterKeyCallback gGetRPKMasterKeyCallback = NULL; -void SetInputRPKMasterKeyCB(GetRPKMasterKeyCallback rpkCB) +OCStackResult SetRPKMasterKeyCB(GetRPKMasterKeyCallback rpkCB) { if(NULL == rpkCB) { OIC_LOG(ERROR, TAG, "Failed to set callback for rpk."); - return; + return OC_STACK_INVALID_PARAM; } gGetRPKMasterKeyCallback = rpkCB; + + return OC_STACK_OK; } -void UnsetRPKMasterKeyCB() +OCStackResult UnsetRPKMasterKeyCB() { gGetRPKMasterKeyCallback = NULL; + + return OC_STACK_OK; } OCStackResult CreateRPKBasedSelectOxmPayload(OTMContext_t* otmCtx, uint8_t **payload, size_t *size) @@ -102,28 +106,31 @@ OCStackResult CreateRPKBasedOwnerTransferPayload(OTMContext_t* otmCtx, uint8_t * return DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm, payload, size, true); } -OCStackResult GetMasterRPK(char* master, size_t* master_len) +OCStackResult GetMasterRPK() { - if(!master || !master_len) - { - OIC_LOG(ERROR, TAG, "RPK buffer is NULL"); - return OC_STACK_INVALID_PARAM; - } - - OIC_LOG(DEBUG, TAG, "Invoking gGetRPKMasterKeyCallback"); - if(gGetRPKMasterKeyCallback) + if (gGetRPKMasterKeyCallback) { - gGetRPKMasterKeyCallback(g_RPKOxmData.rpkData, &g_RPKOxmData.rpkSize); - if (g_RPKOxmData.rpkSize > OXM_RPK_MASTER_KEY_MAX_SIZE) + OIC_LOG(DEBUG, TAG, "Invoking gGetRPKMasterKeyCallback"); + char *rpkData = NULL; + size_t rpkSize = 0; + gGetRPKMasterKeyCallback(&rpkData, &rpkSize); + if (!rpkData) + { + OIC_LOG(ERROR, TAG, "RPK is NULL"); + return OC_STACK_ERROR; + } + if (rpkSize > OXM_RPK_MASTER_KEY_MAX_SIZE) { - OIC_LOG(ERROR, TAG, "Buffer overflow: RPK master key must not exceed OXM_RPK_MASTER_KEY_MAX_SIZE"); + OIC_LOG(ERROR, TAG, "RPK master key must not exceed OXM_RPK_MASTER_KEY_MAX_SIZE"); return OC_STACK_ERROR; } + g_RPKOxmData.rpkSize = rpkSize; + memcpy(g_RPKOxmData.rpkData, rpkData, g_RPKOxmData.rpkSize); } else { OIC_LOG(ERROR, TAG, "Invoke RPK callback failed!"); - OIC_LOG(ERROR, TAG, "Callback for input RPK should be registered to use Random RPK based OxM."); + OIC_LOG(ERROR, TAG, "Callback for input RPK master key should be registered to use RPK based OxM."); return OC_STACK_ERROR; } @@ -217,11 +224,9 @@ OCStackResult GetMasterRPKCallback(OTMContext_t *otmCtx) return OC_STACK_INVALID_PARAM; } - uint8_t rpkData[OXM_RPK_MASTER_KEY_MAX_SIZE + 1] = {0}; - size_t rpkLen = 0; OCStackResult res = OC_STACK_ERROR; - res = GetMasterRPK((char*)rpkData, &rpkLen); + res = GetMasterRPK(); if (OC_STACK_OK != res) { diff --git a/resource/csdk/security/src/oxmpincommon.c b/resource/csdk/security/src/oxmpincommon.c index 7b6dce1..3646605 100644 --- a/resource/csdk/security/src/oxmpincommon.c +++ b/resource/csdk/security/src/oxmpincommon.c @@ -124,7 +124,6 @@ void SetClosePinDisplayCB(ClosePinDisplayCallback closeCB) gClosePinDispalyCallback = closeCB; } - void UnsetInputPinCB() { gInputPinCallback = NULL; @@ -313,8 +312,7 @@ void SetUuidForPinBasedOxm(const OicUuid_t* uuid) int DerivePSKUsingPIN(uint8_t* result) { - int dtlsRes = DeriveCryptoKeyFromPassword( - (const unsigned char *)g_PinOxmData.pinData, + int dtlsRes = DeriveCryptoKeyFromPassword((const unsigned char *)g_PinOxmData.pinData, g_PinOxmData.pinSize, g_PinOxmData.newDevice.id, UUID_LENGTH, PBKDF_ITERATIONS, -- 2.7.4