1 /* *****************************************************************
3 * Copyright 2017 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 * *****************************************************************/
22 #include "srmresourcestrings.h"
23 #include "oic_malloc.h"
25 #include "psinterface.h"
27 #include "doxmresource.h"
28 #include "svrdbeditordoxm.h"
31 #define DOXM_TYPE_CNT (6)
33 #define DOXM_TYPE_CNT (5)
34 #endif //MULTIPLE_OWNER
36 static const char *DOXM_TYPE_STRING[DOXM_TYPE_CNT] =
38 "OCFJustWorks", "OCFSharedPin", "OCFMfgCert",
41 #endif //MULTIPLE_OWNER
42 "MV_JUST_WORKS", "CON_MFG_CERT"
45 static const unsigned int DOXM_TYPE_NUMBER[DOXM_TYPE_CNT] =
47 OIC_JUST_WORKS, OIC_RANDOM_DEVICE_PIN, OIC_MANUFACTURER_CERTIFICATE,
50 #endif //MULTIPLE_OWNER
51 OIC_MV_JUST_WORKS, OIC_CON_MFG_CERT
54 #define DOXM_SCT_CNT (6)
55 static const char *DOXM_SCT_STRING[DOXM_SCT_CNT] =
57 "SYMMETRIC_PAIR_WISE_KEY", "SYMMETRIC_GROUP_KEY", "ASYMMETRIC_KEY",
58 "SIGNED_ASYMMETRIC_KEY", "PIN_PASSWORD", "ASYMMETRIC_ENCRYPTION_KEY"
60 static const unsigned int DOXM_SCT_NUMBER[DOXM_SCT_CNT] =
62 SYMMETRIC_PAIR_WISE_KEY, SYMMETRIC_GROUP_KEY, ASYMMETRIC_KEY, SIGNED_ASYMMETRIC_KEY,
63 PIN_PASSWORD, ASYMMETRIC_ENCRYPTION_KEY
66 typedef enum DoxmModifyType
74 DOXM_EDIT_ROWNERID = 7,
79 static OicSecDoxm_t *g_doxm = NULL;
83 DeleteDoxmBinData(g_doxm);
87 int GetDoxmDevID(OicUuid_t *deviceuuid)
89 if (deviceuuid && g_doxm)
91 *deviceuuid = g_doxm->deviceID;
98 static void PrintMom(const OicSecMom_t *mom)
102 PRINT_DATA("%d (", mom->mode);
106 case OIC_MULTIPLE_OWNER_DISABLE:
107 PRINT_DATA(" OIC_MULTIPLE_OWNER_DISABLE ");
109 case OIC_MULTIPLE_OWNER_ENABLE:
110 PRINT_DATA(" OIC_MULTIPLE_OWNER_ENABLE ");
112 case OIC_MULTIPLE_OWNER_TIMELY_ENABLE:
113 PRINT_DATA(" OIC_MULTIPLE_OWNER_TIMELY_ENABLE ");
123 PRINT_DATA("NULL\n");
128 void RefreshDoxm(void)
130 OCStackResult ocResult = OC_STACK_ERROR;
131 OicSecDoxm_t *tmpDoxm = NULL;
132 uint8_t *secPayload = NULL;
133 size_t payloadSize = 0;
135 ocResult = GetSecureVirtualDatabaseFromPS(OIC_JSON_DOXM_NAME, &secPayload, &payloadSize);
136 if (OC_STACK_OK != ocResult)
138 PRINT_WARN("GetSecureVirtualDatabaseFromPS : %d", ocResult);
141 ocResult = CBORPayloadToDoxm(secPayload, payloadSize, &tmpDoxm);
142 if (OC_STACK_OK != ocResult)
144 PRINT_ERR("Failed CBORPayloadToDoxm");
157 static void UpdateDoxm(void)
159 OCStackResult doxmResult = OC_STACK_ERROR;
160 uint8_t *doxmPayload = NULL;
161 size_t doxmPayloadSize = 0;
163 doxmResult = DoxmToCBORPayload(g_doxm, &doxmPayload, &doxmPayloadSize, false);
164 if (OC_STACK_OK != doxmResult)
166 PRINT_ERR("doxmToCBORPayload error : %d", doxmResult);
169 doxmResult = UpdateSecureResourceInPS(OIC_JSON_DOXM_NAME, doxmPayload, doxmPayloadSize);
170 if (OC_STACK_OK != doxmResult)
172 PRINT_ERR("UpdateSecureResourceInPS error : %d", doxmResult);
173 OICFree(doxmPayload);
176 OICFree(doxmPayload);
181 PRINT_INFO("\n\n********************* [%-20s] *********************",
185 PRINT_PROG("%15s : ", OIC_JSON_OWNED_NAME);
186 (g_doxm->owned ? PrintString("True (Owned)") : PrintString("False (Unowned)"));
188 PRINT_PROG("%15s : ", OIC_JSON_OXMS_NAME);
189 PrintIntArray((int *)g_doxm->oxm, g_doxm->oxmLen);
191 PRINT_PROG("%15s : ", OIC_JSON_OXM_SEL_NAME);
192 PrintInt((int)g_doxm->oxmSel);
194 PRINT_PROG("%15s : ", OIC_JSON_SUPPORTED_CRED_TYPE_NAME);
195 PrintInt((int)g_doxm->sct);
197 #ifdef MULTIPLE_OWNER
198 PRINT_PROG("%15s : ", OIC_JSON_MOM_NAME);
199 PrintMom(g_doxm->mom);
201 // TODO: Print Subowner List
202 #endif //MULTIPLE_OWNER
204 PRINT_PROG("%15s : ", OIC_JSON_DEVICE_ID_NAME);
205 PrintUuid(&g_doxm->deviceID);
207 PRINT_PROG("%15s : ", OIC_JSON_DEVOWNERID_NAME);
208 PrintUuid(&g_doxm->owner);
210 PRINT_PROG("%15s : ", OIC_JSON_ROWNERID_NAME);
211 PrintUuid(&g_doxm->rownerID);
215 PRINT_PROG("doxm is empty.\n");
218 PRINT_INFO("********************* [%-20s] *********************",
222 static int InputOxm(OicSecOxm_t **oxm, size_t *oxmLen)
224 bool oxmSelected[DOXM_TYPE_CNT] = {false};
225 unsigned int sel = 0;
226 unsigned int *tmpOxm = NULL;
230 PRINT_PROG("\nInput value identifying the owner transfer method\n");
234 for (size_t i = 0; i < DOXM_TYPE_CNT; i++)
238 PRINT_DATA("\t%zu. %s\n", i, DOXM_TYPE_STRING[i]);
242 PRINT_NORMAL("\t%zu. %s\n", i, DOXM_TYPE_STRING[i]);
245 PRINT_NORMAL("\t%d. Save\n", DOXM_SAVE);
246 PRINT_NORMAL("\t%d. Cancel\n", DOXM_CANCEL);
247 sel = InputNumber("Select number : ");
248 if (sel < DOXM_TYPE_CNT)
250 if (oxmSelected[sel])
253 oxmSelected[sel] = false;
258 oxmSelected[sel] = true;
261 else if (DOXM_SAVE == sel)
265 else if (DOXM_CANCEL == sel)
271 PRINT_WARN("Wrong number.");
274 tmpOxm = (unsigned int *)OICCalloc(tmpLen, sizeof(unsigned int));
277 PRINT_ERR("Failed to allocate memory");
280 for (size_t i = 0; i < DOXM_TYPE_CNT; i++)
284 tmpOxm[oxmIdx++] = DOXM_TYPE_NUMBER[i];
296 static int InputOxmSel(OicSecOxm_t *oxms, size_t oxmLen, OicSecOxm_t *oxmSel)
298 bool oxmSelected[DOXM_TYPE_CNT] = {false};
299 unsigned int sel = 0;
301 for (size_t i = 0; i < oxmLen; i++)
303 for (size_t j = 0; j < DOXM_TYPE_CNT; j++)
305 if (DOXM_TYPE_NUMBER[j] == oxms[i])
307 oxmSelected[j] = true;
313 PRINT_PROG("\nInput selected owner transfer method\n");
314 PRINT_INFO("You can select CYAN COLOR methods in oxms\n");
318 for (size_t i = 0; i < DOXM_TYPE_CNT; i++)
322 PRINT_DATA("\t%zu. %s\n", i, DOXM_TYPE_STRING[i]);
326 PRINT_NORMAL("\t%zu. %s\n", i, DOXM_TYPE_STRING[i]);
329 PRINT_NORMAL("\t%d. Cancel\n", DOXM_CANCEL);
330 sel = InputNumber("Select number : ");
331 if (sel < DOXM_TYPE_CNT)
333 if (oxmSelected[sel])
335 *oxmSel = DOXM_TYPE_NUMBER[sel];
340 PRINT_INFO("You can select CYAN COLOR methods in oxms\n");
343 else if (DOXM_CANCEL == sel)
349 PRINT_WARN("Wrong number.");
355 static int InputSct(OicSecCredType_t *sct)
357 unsigned int sel = 0;
358 OicSecCredType_t tmpSct = 0;
359 PRINT_PROG("\nInput the Supported Credential Types\n");
362 for (size_t i = 0; i < DOXM_SCT_CNT; i++)
364 (tmpSct & DOXM_SCT_NUMBER[i]) ? PRINT_DATA("\t%zu. %s\n", i, DOXM_SCT_STRING[i]) :
365 PRINT_NORMAL("\t%zu. %s\n", i, DOXM_SCT_STRING[i]);
367 PRINT_NORMAL("\t%d. Save\n", DOXM_SAVE);
368 PRINT_NORMAL("\t%d. Cancel\n", DOXM_CANCEL);
369 sel = InputNumber("Select number: ");
370 if (sel < DOXM_SCT_CNT)
372 tmpSct ^= DOXM_SCT_NUMBER[sel];
374 else if (DOXM_SAVE == sel)
378 else if (DOXM_CANCEL == sel)
384 PRINT_WARN("Wrong number.");
391 static bool InputOwned(void)
396 PRINT_NORMAL("\tEnter Owned (y/n): ");
397 for (int ret = 0; 1 != ret; )
399 ret = scanf("%c", &ans);
400 for ( ; 0x20 <= getchar(); ); // for removing overflow garbages
401 // '0x20<=code' is character region
403 if ('y' == ans || 'Y' == ans || 'n' == ans || 'N' == ans)
405 ans &= ~0x20; // for masking lower case, 'y/n'
408 PRINT_NORMAL("\tEntered Wrong Answer. Please Enter 'y/n' Again\n");
413 static OicSecDoxm_t *InputDoxmData(void)
415 OicSecDoxm_t *doxm = NULL;
418 doxm = (OicSecDoxm_t *)OICCalloc(1, sizeof(OicSecDoxm_t));
421 PRINT_ERR("Failed to allocate memory");
425 PRINT_PROG("\n\nPlease input the each entity of new doxm.\n");
427 ret = InputOxm(&(doxm->oxm), &(doxm->oxmLen));
430 PRINT_ERR("Failed InputOxm\n");
431 DeleteDoxmBinData(doxm);
434 ret = InputOxmSel(doxm->oxm, doxm->oxmLen, &(doxm->oxmSel));
437 PRINT_ERR("Failed InputOxmSel\n");
438 DeleteDoxmBinData(doxm);
442 ret = InputSct(&(doxm->sct));
445 PRINT_ERR("Failed InputSct\n");
446 DeleteDoxmBinData(doxm);
450 doxm->owned = InputOwned();
453 "\tInput the device UUID (e.g. 61646D69-6E44-6576-6963-655575696430) : ");
454 ret = InputUuid(&(doxm->deviceID));
457 PRINT_ERR("InputUuid error");
458 DeleteDoxmBinData(doxm);
463 "\tInput the owner UUID (e.g. 61646D69-6E44-6576-6963-655575696430) : ");
464 ret = InputUuid(&(doxm->owner));
467 PRINT_ERR("InputUuid error");
468 DeleteDoxmBinData(doxm);
473 "\tInput the ROWNER UUID (e.g. 61646D69-6E44-6576-6963-655575696430) : ");
474 ret = InputUuid(&(doxm->rownerID));
477 PRINT_ERR("InputUuid error");
478 DeleteDoxmBinData(doxm);
485 static int ModifyDoxm(void)
491 PRINT_ERR("Doxm is NULL");
495 PRINT_PROG("\n\nPlease input the attribute you want to modify\n");
496 PRINT_DATA("\t%2d. Edit oxms\n", DOXM_EDIT_OXM);
497 PRINT_DATA("\t%2d. Edit oxmSel\n", DOXM_EDIT_OXMSEL);
498 PRINT_DATA("\t%2d. Edit sct\n", DOXM_EDIT_SCT);
499 PRINT_DATA("\t%2d. Edit owned\n", DOXM_EDIT_OWNED);
500 PRINT_DATA("\t%2d. Edit deviceID\n", DOXM_EDIT_DEVICEID);
501 PRINT_DATA("\t%2d. Edit owner\n", DOXM_EDIT_OWNER);
502 PRINT_DATA("\t%2d. Edit rownerID\n", DOXM_EDIT_ROWNERID);
503 PRINT_DATA("\t%2d. Cancel\n", DOXM_CANCEL);
504 modifyMenu = InputNumber("Select the menu : ");
508 if (0 != InputOxm(&(g_doxm->oxm), &(g_doxm->oxmLen)))
510 PRINT_ERR("Failed InputOxm");
514 case DOXM_EDIT_OXMSEL:
515 if (0 != InputOxmSel(g_doxm->oxm, g_doxm->oxmLen, &(g_doxm->oxmSel)))
517 PRINT_ERR("Failed InputOxmSel");
522 if (0 != InputSct(&(g_doxm->sct)))
524 PRINT_ERR("Failed InputSct");
528 case DOXM_EDIT_OWNED:
529 g_doxm->owned = InputOwned();
531 case DOXM_EDIT_DEVICEID:
533 "\tInput the DEVICE ID (e.g. 61646D69-6E44-6576-6963-655575696430) : ");
534 ret = InputUuid(&(g_doxm->deviceID));
537 PRINT_ERR("InputUuid error");
541 case DOXM_EDIT_OWNER:
543 "\tInput the OWNER ID (e.g. 61646D69-6E44-6576-6963-655575696430) : ");
544 ret = InputUuid(&(g_doxm->owner));
547 PRINT_ERR("InputUuid error");
551 case DOXM_EDIT_ROWNERID:
553 "\tInput the ROWNER ID (e.g. 61646D69-6E44-6576-6963-655575696430) : ");
554 ret = InputUuid(&(g_doxm->rownerID));
557 PRINT_ERR("InputUuid error");
565 PRINT_ERR("Invalid menu");
572 void HandleDoxmOperation(const SubOperationType_t cmd)
574 if (SVR_EDIT_IDX_SIZE <= cmd)
576 PRINT_ERR("Invalid menu");
587 PRINT_ERR("Already exist");
592 g_doxm = InputDoxmData();
595 PRINT_ERR("Failed to InputDoxmData");
605 OCStackResult doxmResult = OC_STACK_ERROR;
607 DeleteDoxmBinData(g_doxm);
609 doxmResult = UpdateSecureResourceInPS(OIC_JSON_DOXM_NAME, NULL, 0);
610 if (OC_STACK_OK != doxmResult)
612 PRINT_ERR("UpdateSecureResourceInPS error : %d", doxmResult);
618 PRINT_ERR("Not exist");
623 if (0 != ModifyDoxm())
625 PRINT_ERR("Failed modification");
628 PRINT_INFO("\n\nDoxm Modified");
633 PRINT_ERR("Invalid menu");