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