OCStackResult InitPstatResource() { OCStackResult ret = OC_STACK_ERROR; // Read Pstat resource from PS uint8_t *data = NULL; size_t size = 0; OicUuid_t emptyUuid = {.id={0}}; ret = GetSecureVirtualDatabaseFromPS(OIC_JSON_PSTAT_NAME, &data, &size); // If database read failed if (OC_STACK_OK != ret) { OIC_LOG (DEBUG, TAG, "ReadSVDataFromPS failed"); } if (data) { // Read ACL resource from PS ret = CBORPayloadToPstat(data, size, &gPstat); OICFree(data); } /* * If SVR database in persistent storage got corrupted or * is not available for some reason, a default pstat is created * which allows user to initiate pstat provisioning again. */ if ((OC_STACK_OK != ret) || !gPstat) { gPstat = GetPstatDefault(); } VERIFY_NON_NULL(TAG, gPstat, FATAL); //In case of Pstat's device id is empty, fill the device id as doxm's device id. if(0 == memcmp(&gPstat->deviceID, &emptyUuid, sizeof(OicUuid_t))) { OicUuid_t doxmUuid = {.id={0}}; if(OC_STACK_OK == GetDoxmDeviceID(&doxmUuid)) { memcpy(&gPstat->deviceID, &doxmUuid, sizeof(OicUuid_t)); } }
/** * Function to save ownerPSK at provisioning tool end. * * @param[in] selectedDeviceInfo selected device information to performing provisioning. * @return OC_STACK_OK on success */ static OCStackResult SaveOwnerPSK(OCProvisionDev_t *selectedDeviceInfo) { OC_LOG(DEBUG, TAG, "IN SaveOwnerPSK"); OCStackResult res = OC_STACK_ERROR; CAEndpoint_t endpoint; memset(&endpoint, 0x00, sizeof(CAEndpoint_t)); OICStrcpy(endpoint.addr, MAX_ADDR_STR_SIZE_CA, selectedDeviceInfo->endpoint.addr); endpoint.addr[MAX_ADDR_STR_SIZE_CA - 1] = '\0'; endpoint.port = selectedDeviceInfo->securePort; OicUuid_t ptDeviceID = {.id={0}}; if (OC_STACK_OK != GetDoxmDeviceID(&ptDeviceID)) { OC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID"); return res; } uint8_t ownerPSK[OWNER_PSK_LENGTH_128] = {0}; //Generating OwnerPSK CAResult_t pskRet = CAGenerateOwnerPSK(&endpoint, (uint8_t *)GetOxmString(selectedDeviceInfo->doxm->oxmSel), strlen(GetOxmString(selectedDeviceInfo->doxm->oxmSel)), ptDeviceID.id, sizeof(ptDeviceID.id), selectedDeviceInfo->doxm->deviceID.id, sizeof(selectedDeviceInfo->doxm->deviceID.id), ownerPSK, OWNER_PSK_LENGTH_128); if (CA_STATUS_OK == pskRet) { OC_LOG(INFO, TAG,"ownerPSK dump:\n"); OC_LOG_BUFFER(INFO, TAG,ownerPSK, OWNER_PSK_LENGTH_128); //Generating new credential for provisioning tool size_t ownLen = 1; uint32_t outLen = 0; char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(ownerPSK)) + 1] = {}; B64Result b64Ret = b64Encode(ownerPSK, sizeof(ownerPSK), base64Buff, sizeof(base64Buff), &outLen); VERIFY_SUCCESS(TAG, B64_OK == b64Ret, ERROR); OicSecCred_t *cred = GenerateCredential(&selectedDeviceInfo->doxm->deviceID, SYMMETRIC_PAIR_WISE_KEY, NULL, base64Buff, ownLen, &ptDeviceID); VERIFY_NON_NULL(TAG, cred, ERROR); res = AddCredential(cred); if(res != OC_STACK_OK) { DeleteCredList(cred); return res; } } else { OC_LOG(ERROR, TAG, "CAGenerateOwnerPSK failed"); } OC_LOG(DEBUG, TAG, "OUT SaveOwnerPSK"); exit: return res; }
OCStackResult SRPProvisionCredentials(void *ctx, OicSecCredType_t type, size_t keySize, const OCProvisionDev_t *pDev1, const OCProvisionDev_t *pDev2, OCProvisionResultCB resultCallback) { VERIFY_NON_NULL(TAG, pDev1, ERROR, OC_STACK_INVALID_PARAM); VERIFY_NON_NULL(TAG, pDev2, ERROR, OC_STACK_INVALID_PARAM); VERIFY_NON_NULL(TAG, resultCallback, ERROR, OC_STACK_INVALID_CALLBACK); if (!(keySize == OWNER_PSK_LENGTH_128 || keySize == OWNER_PSK_LENGTH_256)) { OC_LOG(INFO, TAG, "Invalid key size"); return OC_STACK_INVALID_PARAM; } OC_LOG(INFO, TAG, "In SRPProvisionCredentials"); OicUuid_t provTooldeviceID = {{0,}}; if (OC_STACK_OK != GetDoxmDeviceID(&provTooldeviceID)) { OC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID"); return OC_STACK_ERROR; } OC_LOG(INFO, TAG, "retrieved deviceid"); switch (type) { case SYMMETRIC_PAIR_WISE_KEY: { const OCProvisionDev_t *firstDevice = pDev1; const OCProvisionDev_t *secondDevice = pDev2; OicSecCred_t *firstCred = NULL; OicSecCred_t *secondCred = NULL; OCStackResult res = PMGeneratePairWiseCredentials(type, keySize, &provTooldeviceID, &firstDevice->doxm->deviceID, &secondDevice->doxm->deviceID, &firstCred, &secondCred); VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR); OC_LOG(INFO, TAG, "Credentials generated successfully"); CredentialData_t *credData = (CredentialData_t *) OICMalloc(sizeof(CredentialData_t)); if (NULL == credData) { OC_LOG(ERROR, TAG, "Memory allocation problem"); return OC_STACK_NO_MEMORY; } memset(credData, 0x00, sizeof(CredentialData_t)); credData->deviceInfo1 = firstDevice; credData->deviceInfo2 = secondDevice; credData->credInfo = secondCred; credData->ctx = ctx; credData->credInfoFirst = firstCred; credData->numOfResults = 0; credData->resultCallback = resultCallback; // first call to provision creds to device1. // second call to provision creds to device2. int noOfRiCalls = 2; credData->resArr = (OCProvisionResult_t*)OICMalloc(sizeof(OCProvisionResult_t) * noOfRiCalls); if (NULL == credData->resArr) { OC_LOG(ERROR, TAG, "Memory allocation problem"); return OC_STACK_NO_MEMORY; } memset(credData->resArr, 0x00, sizeof(sizeof(OCProvisionResult_t)*noOfRiCalls)); res = provisionCredentials(firstCred, firstDevice, credData, &provisionCredentialCB1); if (OC_STACK_OK != res) { DeleteCredList(firstCred); DeleteCredList(secondCred); OICFree(credData->resArr); OICFree(credData); } OC_LOG_V(INFO, TAG, "provisionCredentials returned: %d",res); VERIFY_SUCCESS(TAG, (res==OC_STACK_OK), ERROR, OC_STACK_ERROR); return res; } default: { OC_LOG(ERROR, TAG, "Invalid option."); return OC_STACK_INVALID_PARAM; } } return OC_STACK_ERROR; }