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