Imported Upstream version 1.1.0
[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 OCStackResult CreateJustWorksSelectOxmPayload(OTMContext_t *otmCtx, uint8_t **payload, size_t *size)
36 {
37     if (!otmCtx || !otmCtx->selectedDeviceInfo || !payload || *payload || !size)
38     {
39         return OC_STACK_INVALID_PARAM;
40     }
41
42     otmCtx->selectedDeviceInfo->doxm->oxmSel = OIC_JUST_WORKS;
43     *payload = NULL;
44     *size = 0;
45
46     return DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm, payload, size);
47 }
48
49 OCStackResult CreateJustWorksOwnerTransferPayload(OTMContext_t* otmCtx, uint8_t **payload, size_t *size)
50 {
51     if (!otmCtx || !otmCtx->selectedDeviceInfo || !payload || *payload || !size)
52     {
53         return OC_STACK_INVALID_PARAM;
54     }
55
56     OicUuid_t uuidPT = {.id={0}};
57
58     if (OC_STACK_OK != GetDoxmDeviceID(&uuidPT))
59     {
60         OIC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
61         return OC_STACK_ERROR;
62     }
63     memcpy(otmCtx->selectedDeviceInfo->doxm->owner.id, uuidPT.id , UUID_LENGTH);
64
65     *payload = NULL;
66     *size = 0;
67
68     return DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm, payload, size);
69 }
70
71 OCStackResult LoadSecretJustWorksCallback(OTMContext_t* UNUSED_PARAM)
72 {
73     //In case of 'just works', secret data not required
74     (void)UNUSED_PARAM;
75     return OC_STACK_OK;
76 }
77
78 OCStackResult CreateSecureSessionJustWorksCallback(OTMContext_t* otmCtx)
79 {
80     OIC_LOG(INFO, TAG, "IN CreateSecureSessionJustWorksCallback");
81     if (!otmCtx || !otmCtx->selectedDeviceInfo)
82     {
83         return OC_STACK_INVALID_PARAM;
84     }
85
86     CAResult_t caresult = CAEnableAnonECDHCipherSuite(true);
87     if (CA_STATUS_OK != caresult)
88     {
89         OIC_LOG_V(ERROR, TAG, "Unable to enable anon cipher suite");
90         return OC_STACK_ERROR;
91     }
92     OIC_LOG(INFO, TAG, "Anonymous cipher suite Enabled.");
93
94     caresult  = CASelectCipherSuite(TLS_ECDH_anon_WITH_AES_128_CBC_SHA_256);
95     if (CA_STATUS_OK != caresult)
96     {
97         OIC_LOG_V(ERROR, TAG, "Failed to select TLS_ECDH_anon_WITH_AES_128_CBC_SHA_256");
98         caresult = CAEnableAnonECDHCipherSuite(false);
99         if (CA_STATUS_OK != caresult)
100         {
101             OIC_LOG_V(ERROR, TAG, "Unable to enable anon cipher suite");
102         }
103         else
104         {
105             OIC_LOG(INFO, TAG, "Anonymous cipher suite Disabled.");
106         }
107         return OC_STACK_ERROR;
108     }
109     OIC_LOG(INFO, TAG, "TLS_ECDH_anon_WITH_AES_128_CBC_SHA_256 cipher suite selected.");
110
111     OCProvisionDev_t *selDevInfo = otmCtx->selectedDeviceInfo;
112     CAEndpoint_t *endpoint = (CAEndpoint_t *)OICCalloc(1, sizeof (CAEndpoint_t));
113     if(NULL == endpoint)
114     {
115         return OC_STACK_NO_MEMORY;
116     }
117     memcpy(endpoint, &selDevInfo->endpoint, sizeof(CAEndpoint_t));
118     endpoint->port = selDevInfo->securePort;
119
120     caresult = CAInitiateHandshake(endpoint);
121     OICFree(endpoint);
122     if (CA_STATUS_OK != caresult)
123     {
124         OIC_LOG_V(ERROR, TAG, "DTLS handshake failure.");
125         return OC_STACK_ERROR;
126     }
127
128     OIC_LOG(INFO, TAG, "OUT CreateSecureSessionJustWorksCallback");
129     return OC_STACK_OK;
130 }