replace : iotivity -> iotivity-sec
[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 "pmtypes.h"
30 #include "ownershiptransfermanager.h"
31
32 #define TAG "OIC_OXM_JustWorks"
33
34 OCStackResult CreateJustWorksSelectOxmPayload(OTMContext_t *otmCtx, uint8_t **payload, size_t *size)
35 {
36     if (!otmCtx || !otmCtx->selectedDeviceInfo || !payload || *payload || !size)
37     {
38         return OC_STACK_INVALID_PARAM;
39     }
40
41     otmCtx->selectedDeviceInfo->doxm->oxmSel = OIC_JUST_WORKS;
42     *payload = NULL;
43     *size = 0;
44
45     return DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm, payload, size, true);
46 }
47
48 OCStackResult CreateJustWorksOwnerTransferPayload(OTMContext_t* otmCtx, uint8_t **payload, size_t *size)
49 {
50     if (!otmCtx || !otmCtx->selectedDeviceInfo || !payload || *payload || !size)
51     {
52         return OC_STACK_INVALID_PARAM;
53     }
54
55     OicUuid_t uuidPT = {.id={0}};
56
57     if (OC_STACK_OK != GetDoxmDeviceID(&uuidPT))
58     {
59         OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
60         return OC_STACK_ERROR;
61     }
62     memcpy(otmCtx->selectedDeviceInfo->doxm->owner.id, uuidPT.id , UUID_LENGTH);
63
64     *payload = NULL;
65     *size = 0;
66
67     return DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm, payload, size, true);
68 }
69
70 OCStackResult LoadSecretJustWorksCallback(OTMContext_t* UNUSED_PARAM)
71 {
72     //In case of 'just works', secret data not required
73     (void)UNUSED_PARAM;
74     return OC_STACK_OK;
75 }
76
77 OCStackResult CreateSecureSessionJustWorksCallback(OTMContext_t* otmCtx)
78 {
79     OIC_LOG(INFO, TAG, "IN CreateSecureSessionJustWorksCallback");
80     if (!otmCtx || !otmCtx->selectedDeviceInfo)
81     {
82         return OC_STACK_INVALID_PARAM;
83     }
84
85     CAResult_t caresult = CAEnableAnonECDHCipherSuite(true);
86     if (CA_STATUS_OK != caresult)
87     {
88         OIC_LOG_V(ERROR, TAG, "Unable to enable anon cipher suite");
89         return OC_STACK_ERROR;
90     }
91     OIC_LOG(INFO, TAG, "Anonymous cipher suite Enabled.");
92
93     caresult  = CASelectCipherSuite(MBEDTLS_TLS_ECDH_ANON_WITH_AES_128_CBC_SHA256, otmCtx->selectedDeviceInfo->endpoint.adapter);
94     if (CA_STATUS_OK != caresult)
95     {
96         OIC_LOG_V(ERROR, TAG, "Failed to select TLS_ECDH_anon_WITH_AES_128_CBC_SHA256");
97         caresult = CAEnableAnonECDHCipherSuite(false);
98         if (CA_STATUS_OK != caresult)
99         {
100             OIC_LOG_V(ERROR, TAG, "Unable to enable anon cipher suite");
101         }
102         else
103         {
104             OIC_LOG(INFO, TAG, "Anonymous cipher suite Disabled.");
105         }
106         return OC_STACK_ERROR;
107     }
108     OIC_LOG(INFO, TAG, "TLS_ECDH_anon_WITH_AES_128_CBC_SHA256 cipher suite selected.");
109
110     OCProvisionDev_t *selDevInfo = otmCtx->selectedDeviceInfo;
111     CAEndpoint_t endpoint;
112     memcpy(&endpoint, &selDevInfo->endpoint, sizeof(CAEndpoint_t));
113
114     if(CA_ADAPTER_IP == endpoint.adapter)
115     {
116         endpoint.port = selDevInfo->securePort;
117         caresult = CAInitiateHandshake(&endpoint);
118     }
119     else if (CA_ADAPTER_GATT_BTLE == endpoint.adapter)
120     {
121         caresult = CAInitiateHandshake(&endpoint);
122     }
123 #ifdef __WITH_TLS__
124     else
125     {
126         endpoint.port = selDevInfo->tcpPort;
127         caresult = CAinitiateSslHandshake(&endpoint);
128     }
129 #endif
130     if (CA_STATUS_OK != caresult)
131     {
132         OIC_LOG_V(ERROR, TAG, "DTLS/TLS handshake failure.");
133         return OC_STACK_ERROR;
134     }
135
136     OIC_LOG(INFO, TAG, "OUT CreateSecureSessionJustWorksCallback");
137     return OC_STACK_OK;
138 }
139
140 OCStackResult CreateMVJustWorksSelectOxmPayload(OTMContext_t *otmCtx, uint8_t **cborPayload,
141                                              size_t *cborSize)
142 {
143     if (!otmCtx || !otmCtx->selectedDeviceInfo || !cborPayload || *cborPayload || !cborSize)
144     {
145         return OC_STACK_INVALID_PARAM;
146     }
147
148     otmCtx->selectedDeviceInfo->doxm->oxmSel = OIC_MV_JUST_WORKS;
149     *cborPayload = NULL;
150     *cborSize = 0;
151
152     return DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm, cborPayload, cborSize, true);
153 }
154