replaced OC_LOG with OIC_LOG in security
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / src / oxmjustworks.c
1 /* *****************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * *****************************************************************/
20
21 #include <memory.h>
22 #include "ocstack.h"
23 #include "securevirtualresourcetypes.h"
24 #include "doxmresource.h"
25 #include "cacommon.h"
26 #include "cainterface.h"
27 #include "oic_malloc.h"
28 #include "logger.h"
29 #include "global.h"
30 #include "pmtypes.h"
31 #include "ownershiptransfermanager.h"
32
33 #define TAG "OXM_JustWorks"
34
35 char* CreateJustWorksSelectOxmPayload(OTMContext_t* otmCtx)
36 {
37     if(!otmCtx || !otmCtx->selectedDeviceInfo)
38     {
39         return NULL;
40     }
41
42     otmCtx->selectedDeviceInfo->doxm->oxmSel = OIC_JUST_WORKS;
43     return BinToDoxmJSON(otmCtx->selectedDeviceInfo->doxm);
44 }
45
46 char* CreateJustWorksOwnerTransferPayload(OTMContext_t* otmCtx)
47 {
48     if(!otmCtx || !otmCtx->selectedDeviceInfo)
49     {
50         return NULL;
51     }
52
53     OicUuid_t uuidPT = {.id={0}};
54
55     if (OC_STACK_OK != GetDoxmDeviceID(&uuidPT))
56     {
57         OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
58         return NULL;
59     }
60     memcpy(otmCtx->selectedDeviceInfo->doxm->owner.id, uuidPT.id , UUID_LENGTH);
61     otmCtx->selectedDeviceInfo->doxm->owned = true;
62
63     return BinToDoxmJSON(otmCtx->selectedDeviceInfo->doxm);
64 }
65
66 OCStackResult LoadSecretJustWorksCallback(OTMContext_t* UNUSED_PARAM)
67 {
68     //In case of 'just works', secret data not required
69     (void)UNUSED_PARAM;
70     return OC_STACK_OK;
71 }
72
73 OCStackResult CreateSecureSessionJustWorksCallback(OTMContext_t* otmCtx)
74 {
75     OIC_LOG(INFO, TAG, "IN CreateSecureSessionJustWorksCallback");
76     if(!otmCtx || !otmCtx->selectedDeviceInfo)
77     {
78         return OC_STACK_INVALID_PARAM;
79     }
80
81     CAResult_t caresult = CAEnableAnonECDHCipherSuite(true);
82     if (CA_STATUS_OK != caresult)
83     {
84         OIC_LOG_V(ERROR, TAG, "Unable to enable anon cipher suite");
85         return OC_STACK_ERROR;
86     }
87     OIC_LOG(INFO, TAG, "Anonymous cipher suite Enabled.");
88
89     caresult  = CASelectCipherSuite(TLS_ECDH_anon_WITH_AES_128_CBC_SHA_256);
90     if (CA_STATUS_OK != caresult)
91     {
92         OIC_LOG_V(ERROR, TAG, "Failed to select TLS_ECDH_anon_WITH_AES_128_CBC_SHA_256");
93         caresult = CAEnableAnonECDHCipherSuite(false);
94         if (CA_STATUS_OK != caresult)
95         {
96             OIC_LOG_V(ERROR, TAG, "Unable to enable anon cipher suite");
97         }
98         else
99         {
100             OIC_LOG(INFO, TAG, "Anonymous cipher suite Disabled.");
101         }
102         return OC_STACK_ERROR;
103     }
104     OIC_LOG(INFO, TAG, "TLS_ECDH_anon_WITH_AES_128_CBC_SHA_256 cipher suite selected.");
105
106     OCProvisionDev_t* selDevInfo = otmCtx->selectedDeviceInfo;
107     CAEndpoint_t *endpoint = (CAEndpoint_t *)OICCalloc(1, sizeof (CAEndpoint_t));
108     if(NULL == endpoint)
109     {
110         return OC_STACK_NO_MEMORY;
111     }
112     memcpy(endpoint,&selDevInfo->endpoint,sizeof(CAEndpoint_t));
113     endpoint->port = selDevInfo->securePort;
114
115     caresult = CAInitiateHandshake(endpoint);
116     OICFree(endpoint);
117     if (CA_STATUS_OK != caresult)
118     {
119         OIC_LOG_V(ERROR, TAG, "DTLS handshake failure.");
120         return OC_STACK_ERROR;
121     }
122
123     OIC_LOG(INFO, TAG, "OUT CreateSecureSessionJustWorksCallback");
124     return OC_STACK_OK;
125 }