1 /* *****************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * *****************************************************************/
26 #include "pinoxmcommon.h"
28 #include "securevirtualresourcetypes.h"
30 #define TAG "PIN_OXM_COMMON"
32 static GeneratePinCallback gGenPinCallback = NULL;
33 static InputPinCallback gInputPinCallback = NULL;
35 typedef struct PinOxmData {
36 uint8_t pinData[OXM_RANDOM_PIN_SIZE + 1];
40 static PinOxmData_t g_PinOxmData;
43 void SetInputPinCB(InputPinCallback pinCB)
47 OIC_LOG(ERROR, TAG, "Failed to set callback for input pin.");
51 gInputPinCallback = pinCB;
54 void SetGeneratePinCB(GeneratePinCallback pinCB)
58 OIC_LOG(ERROR, TAG, "Failed to set callback for generate pin.");
62 gGenPinCallback = pinCB;
65 OCStackResult GeneratePin(char* pinBuffer, size_t bufferSize)
69 OIC_LOG(ERROR, TAG, "PIN buffer is NULL");
70 return OC_STACK_INVALID_PARAM;
72 if(OXM_RANDOM_PIN_SIZE + 1 > bufferSize)
74 OIC_LOG(ERROR, TAG, "PIN buffer size is too small");
75 return OC_STACK_INVALID_PARAM;
77 for(size_t i = 0; i < OXM_RANDOM_PIN_SIZE; i++)
79 pinBuffer[i] = OCGetRandomRange((uint32_t)'0', (uint32_t)'9');
80 g_PinOxmData.pinData[i] = pinBuffer[i];
82 pinBuffer[OXM_RANDOM_PIN_SIZE] = '\0';
83 g_PinOxmData.pinData[OXM_RANDOM_PIN_SIZE] = '\0';
87 gGenPinCallback(pinBuffer, OXM_RANDOM_PIN_SIZE);
91 OIC_LOG(ERROR, TAG, "Invoke PIN callback failed!");
92 OIC_LOG(ERROR, TAG, "Callback for genrate PIN should be registered to use PIN based OxM.");
93 return OC_STACK_ERROR;
100 OCStackResult InputPin(char* pinBuffer, size_t bufferSize)
104 OIC_LOG(ERROR, TAG, "PIN buffer is NULL");
105 return OC_STACK_INVALID_PARAM;
107 if(OXM_RANDOM_PIN_SIZE + 1 > bufferSize)
109 OIC_LOG(ERROR, TAG, "PIN buffer size is too small");
110 return OC_STACK_INVALID_PARAM;
113 if(gInputPinCallback)
115 gInputPinCallback(pinBuffer, OXM_RANDOM_PIN_SIZE + 1);
116 memcpy(g_PinOxmData.pinData, pinBuffer, OXM_RANDOM_PIN_SIZE);
117 g_PinOxmData.pinData[OXM_RANDOM_PIN_SIZE] = '\0';
121 OIC_LOG(ERROR, TAG, "Invoke PIN callback failed!");
122 OIC_LOG(ERROR, TAG, "Callback for input PIN should be registered to use PIN based OxM.");
123 return OC_STACK_ERROR;
131 void SetUuidForRandomPinOxm(const OicUuid_t* uuid)
135 memcpy(g_PinOxmData.newDevice.id, uuid->id, UUID_LENGTH);
139 int32_t GetDtlsPskForRandomPinOxm( CADtlsPskCredType_t type,
140 const unsigned char *UNUSED1, size_t UNUSED2,
141 unsigned char *result, size_t result_length)
148 if (NULL == result || result_length < OWNER_PSK_LENGTH_128)
155 case CA_DTLS_PSK_HINT:
156 case CA_DTLS_PSK_IDENTITY:
158 * The server will provide PSK hint to identify PSK according to RFC 4589 and RFC 4279.
160 * At this point, The server generate random hint and
161 * provide it to client through server key exchange message.
163 OCFillRandomMem(result, result_length);
166 OIC_LOG(DEBUG, TAG, "PSK HINT : ");
167 OIC_LOG_BUFFER(DEBUG, TAG, result, result_length);
170 case CA_DTLS_PSK_KEY:
172 int dtlsRes = DeriveCryptoKeyFromPassword(
173 (const unsigned char *)g_PinOxmData.pinData,
175 g_PinOxmData.newDevice.id,
176 UUID_LENGTH, PBKDF_ITERATIONS,
177 OWNER_PSK_LENGTH_128, (uint8_t*)result);
179 OIC_LOG_V(DEBUG, TAG, "DeriveCryptoKeyFromPassword Completed (%d)", dtlsRes);
180 OIC_LOG_V(DEBUG, TAG, "PIN : %s", g_PinOxmData.pinData);
181 OIC_LOG(DEBUG, TAG, "UUID : ");
182 OIC_LOG_BUFFER(DEBUG, TAG, g_PinOxmData.newDevice.id, UUID_LENGTH);
186 ret = OWNER_PSK_LENGTH_128;
190 OIC_LOG_V(ERROR, TAG, "Failed to derive crypto key from PIN : result=%d", dtlsRes);
198 OIC_LOG (ERROR, TAG, "Wrong value passed for CADtlsPskCredType_t.");
206 #endif //__WITH_DTLS__