Exemplo n.º 1
0
JNIEXPORT void JNICALL
Java_org_iotivity_ca_service_RMInterface_RMInitialize(JNIEnv *env, jobject obj, jobject context)
{
    LOGI("RMInitialize");
    if (!env || !obj || !context)
    {
        LOGI("Invalid input parameter");
        return;
    }

    //Currently set context for Android Platform
    CANativeJNISetContext(env, context);

    CAResult_t res = CAInitialize();

    if (CA_STATUS_OK != res)
    {
        LOGE("Could not Initialize");
    }

#ifdef __WITH_DTLS__
    if (CA_STATUS_OK != SetCredentials())
    {
        LOGE("SetCredentials failed");
        return;
    }

    res = CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials);
    if(CA_STATUS_OK != res)
    {
        LOGE("Set credential handler fail");
        return;
    }
#endif
}
Exemplo n.º 2
0
int main()
{
    int ret = system("clear");
    // shell invoke error: 127, others: -1
    if (SYSTEM_INVOKE_ERROR == ret || SYSTEM_ERROR == ret)
    {
        printf("Terminal Clear Error: %d\n", ret);
        return -1;
    }

    printf("=============================================\n");
    printf("\t\tsample main\n");
    printf("=============================================\n");

    CAResult_t res = CAInitialize();
    if (CA_STATUS_OK != res)
    {
        printf("CAInitialize fail\n");
        return -1;
    }

    /*
    * Read DTLS PSK credentials from persistent storage and
    * set in the OC stack.
    */
#ifdef __WITH_DTLS__
    res = SetCredentials();
    if (CA_STATUS_OK != res)
    {
        printf("SetCredentials failed\n");
        return -1;
    }

    res = CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials);
    if (CA_STATUS_OK != res)
    {
        printf("Set credential handler fail\n");
        return -1;
    }
#endif

    // set handler.
    CARegisterHandler(request_handler, response_handler, error_handler);

    process();

    CADestroyToken(g_last_request_token);

    g_last_request_token = NULL;

    CATerminate();
#ifdef __WITH_DTLS__
    clearDtlsCredentialInfo();
#endif
    return 0;
}
Exemplo n.º 3
0
int main()
{
    int ret = system("clear");
    // shell invoke error: 127, others: -1
    if (SYSTEM_INVOKE_ERROR == ret || SYSTEM_ERROR == ret)
    {
        printf("Terminal Clear Error: %d\n", ret);
        return -1;
    }

    printf("=============================================\n");
    printf("\t\tsample main\n");
    printf("=============================================\n");

    CAResult_t res = CAInitialize();
    if (CA_STATUS_OK != res)
    {
        printf("CAInitialize fail\n");
        return -1;
    }

    // Set the PSK Credentials callback handler.
#ifdef __WITH_DTLS__
    res = CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials);
    if (CA_STATUS_OK != res)
    {
        printf("Register credential handler fail\n");
        return -1;
    }
#endif

    // set handler.
    CARegisterHandler(request_handler, response_handler, error_handler);

    process();

    CADestroyToken(g_last_request_token);

    g_last_request_token = NULL;

    CATerminate();
    return 0;
}
Exemplo n.º 4
0
TEST_F(CATests, RegisterDTLSCredentialsHandlerTest)
{
#ifdef __WITH_DTLS__
    EXPECT_EQ(CA_STATUS_OK, CARegisterDTLSCredentialsHandler(CAGetDtlsPskCredentials));
#endif
}
Exemplo n.º 5
0
static OCEntityHandlerResult HandleDoxmPutRequest (const OCEntityHandlerRequest * ehRequest)
{
    OIC_LOG (DEBUG, TAG, "Doxm EntityHandle  processing PUT request");
    OCEntityHandlerResult ehRet = OC_EH_ERROR;
    OicUuid_t emptyOwner = {.id = {0}};

    /*
     * Convert JSON Doxm data into binary. This will also validate
     * the Doxm data received.
     */
    OicSecDoxm_t* newDoxm = JSONToDoxmBin(((OCSecurityPayload*)ehRequest->payload)->securityData);

    if (newDoxm)
    {
        // Iotivity SRM ONLY supports OIC_JUST_WORKS now
        if (OIC_JUST_WORKS == newDoxm->oxmSel)
        {
            if ((false == gDoxm->owned) && (false == newDoxm->owned))
            {
                /*
                 * If current state of the device is un-owned, enable
                 * anonymous ECDH cipher in tinyDTLS so that Provisioning
                 * tool can initiate JUST_WORKS ownership transfer process.
                 */
                if(memcmp(&(newDoxm->owner), &emptyOwner, sizeof(OicUuid_t)) == 0)
                {
                    OIC_LOG (INFO, TAG, "Doxm EntityHandle  enabling AnonECDHCipherSuite");
#ifdef __WITH_DTLS__
                    ehRet = (CAEnableAnonECDHCipherSuite(true) == CA_STATUS_OK) ? OC_EH_OK : OC_EH_ERROR;
#endif //__WITH_DTLS__
                    goto exit;
                }
                else
                {
#ifdef __WITH_DTLS__
                    //Save the owner's UUID to derive owner credential
                    memcpy(&(gDoxm->owner), &(newDoxm->owner), sizeof(OicUuid_t));

//                    OCServerRequest *request = (OCServerRequest *)ehRequest->requestHandle;
//                    //Generating OwnerPSK
//                    OIC_LOG (INFO, TAG, "Doxm EntityHandle  generating OwnerPSK");
//                    //Generate new credential for provisioning tool
//                    ehRet = AddOwnerPSK((CAEndpoint_t *)&request->devAddr, newDoxm,
//                            (uint8_t*) OXM_JUST_WORKS, strlen(OXM_JUST_WORKS));
//                    VERIFY_SUCCESS(TAG, OC_EH_OK == ehRet, ERROR);

                    // Update new state in persistent storage
                    if (true == UpdatePersistentStorage(gDoxm))
                    {
                        ehRet = OC_EH_OK;
                    }
                    else
                    {
                        OIC_LOG(ERROR, TAG, "Failed to update DOXM in persistent storage");
                        ehRet = OC_EH_ERROR;
                    }

                    /*
                     * Disable anonymous ECDH cipher in tinyDTLS since device is now
                     * in owned state.
                     */
                    CAResult_t caRes = CA_STATUS_OK;
                    caRes = CAEnableAnonECDHCipherSuite(false);
                    VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
                    OIC_LOG(INFO, TAG, "ECDH_ANON CipherSuite is DISABLED");

#ifdef __WITH_X509__
#define TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 0xC0AE
                    CASelectCipherSuite(TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8);
#endif //__WITH_X509__
#endif //__WITH_DTLS__
                }
            }
        }
        else if(OIC_RANDOM_DEVICE_PIN == newDoxm->oxmSel)
        {
            if ((false == gDoxm->owned) && (false == newDoxm->owned))
            {
                /*
                 * If current state of the device is un-owned, enable
                 * anonymous ECDH cipher in tinyDTLS so that Provisioning
                 * tool can initiate JUST_WORKS ownership transfer process.
                 */
                if(memcmp(&(newDoxm->owner), &emptyOwner, sizeof(OicUuid_t)) == 0)
                {
                    gDoxm->oxmSel = newDoxm->oxmSel;
                    //Update new state in persistent storage
                    if((UpdatePersistentStorage(gDoxm) == true))
                    {
                        ehRet = OC_EH_OK;
                    }
                    else
                    {
                        OIC_LOG(WARNING, TAG, "Failed to update DOXM in persistent storage");
                        ehRet = OC_EH_ERROR;
                    }

#ifdef __WITH_DTLS__
                    CAResult_t caRes = CA_STATUS_OK;

                    caRes = CAEnableAnonECDHCipherSuite(false);
                    VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
                    OIC_LOG(INFO, TAG, "ECDH_ANON CipherSuite is DISABLED");

                    caRes = CASelectCipherSuite(TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256);
                    VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);

                    char ranPin[OXM_RANDOM_PIN_SIZE + 1] = {0,};
                    if(OC_STACK_OK == GeneratePin(ranPin, OXM_RANDOM_PIN_SIZE + 1))
                    {
                        //Set the device id to derive temporal PSK
                        SetUuidForRandomPinOxm(&gDoxm->deviceID);

                        /**
                         * Since PSK will be used directly by DTLS layer while PIN based ownership transfer,
                         * Credential should not be saved into SVR.
                         * For this reason, use a temporary get_psk_info callback to random PIN OxM.
                         */
                        caRes = CARegisterDTLSCredentialsHandler(GetDtlsPskForRandomPinOxm);
                        VERIFY_SUCCESS(TAG, caRes == CA_STATUS_OK, ERROR);
                        ehRet = OC_EH_OK;
                    }
                    else
                    {
                        OIC_LOG(ERROR, TAG, "Failed to generate random PIN");
                        ehRet = OC_EH_ERROR;
                    }
#endif //__WITH_DTLS__
                }
                else
                {
#ifdef __WITH_DTLS__
                    //Save the owner's UUID to derive owner credential
                    memcpy(&(gDoxm->owner), &(newDoxm->owner), sizeof(OicUuid_t));

                    //Update new state in persistent storage
                    if((UpdatePersistentStorage(gDoxm) == true))
                    {
                        ehRet = OC_EH_OK;
                    }
                    else
                    {
                        OIC_LOG(ERROR, TAG, "Failed to update DOXM in persistent storage");
                        ehRet = OC_EH_ERROR;
                    }
#endif
                }
            }
        }

        /*
         * When current state of the device is un-owned and Provisioning
         * Tool is attempting to change the state to 'Owned' with a
         * qualified value for the field 'Owner'
         */
        if ((false == gDoxm->owned) && (true == newDoxm->owned) &&
            (memcmp(&(gDoxm->owner), &(newDoxm->owner), sizeof(OicUuid_t)) == 0))
        {
            gDoxm->owned = true;
            // Update new state in persistent storage
            if (UpdatePersistentStorage(gDoxm))
            {
                //Update default ACL of security resource to prevent anonymous user access.
                if(OC_STACK_OK == UpdateDefaultSecProvACL())
                {
                    ehRet = OC_EH_OK;
                }
                else
                {
                    OIC_LOG(ERROR, TAG, "Failed to remove default ACL for security provisioning");
                    ehRet = OC_EH_ERROR;
                }
            }
            else
            {
                OIC_LOG(ERROR, TAG, "Failed to update DOXM in persistent storage");
                ehRet = OC_EH_ERROR;
            }
        }
    }

exit:
    if(OC_EH_OK != ehRet)
    {
        OIC_LOG(WARNING, TAG, "The operation failed during handle DOXM request,"\
                            "DOXM will be reverted.");

        /*
         * If some error is occured while ownership transfer,
         * ownership transfer related resource should be revert back to initial status.
         */
        RestoreDoxmToInitState();
        RestorePstatToInitState();
    }

    //Send payload to request originator
    if(OC_STACK_OK != SendSRMResponse(ehRequest, ehRet, NULL))
    {
        OIC_LOG (ERROR, TAG, "SendSRMResponse failed in HandlePstatPostRequest");
    }
    DeleteDoxmBinData(newDoxm);

    return ehRet;
}