Doxm payload conversion from JSON to CBOR
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / src / oxmrandompin.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
23 #include "ocstack.h"
24 #include "securevirtualresourcetypes.h"
25 #include "doxmresource.h"
26 #include "credresource.h"
27 #include "cacommon.h"
28 #include "cainterface.h"
29 #include "ocrandom.h"
30 #include "oic_malloc.h"
31 #include "logger.h"
32 #include "pbkdf2.h"
33 #include "global.h"
34 #include "base64.h"
35 #include "oxmrandompin.h"
36 #include "ownershiptransfermanager.h"
37 #include "pinoxmcommon.h"
38
39 #define TAG "OXM_RandomPIN"
40
41 OCStackResult CreatePinBasedSelectOxmPayload(OTMContext_t* otmCtx, uint8_t **payload, size_t *size)
42 {
43     *payload = NULL;
44     *size = 0;
45     if(!otmCtx || !otmCtx->selectedDeviceInfo || !payload || *payload || !size)
46     {
47         return OC_STACK_INVALID_PARAM;
48     }
49
50     otmCtx->selectedDeviceInfo->doxm->oxmSel = OIC_RANDOM_DEVICE_PIN;
51
52     return DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm, payload, size);
53 }
54
55 OCStackResult CreatePinBasedOwnerTransferPayload(OTMContext_t* otmCtx, uint8_t **payload, size_t *size)
56 {
57     if(!otmCtx || !otmCtx->selectedDeviceInfo || !payload || *payload || !size)
58     {
59         return OC_STACK_INVALID_PARAM;
60     }
61
62     OicUuid_t uuidPT = {.id={0}};
63     *payload = NULL;
64     *size = 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     return DoxmToCBORPayload(otmCtx->selectedDeviceInfo->doxm, payload, size);
74 }
75
76 OCStackResult InputPinCodeCallback(OTMContext_t *otmCtx)
77 {
78     if (!otmCtx || !otmCtx->selectedDeviceInfo)
79     {
80         return OC_STACK_INVALID_PARAM;
81     }
82
83     uint8_t pinData[OXM_RANDOM_PIN_SIZE + 1];
84
85     OCStackResult res = InputPin((char*)pinData, OXM_RANDOM_PIN_SIZE + 1);
86     if (OC_STACK_OK != res)
87     {
88         OIC_LOG(ERROR, TAG, "Failed to input PIN");
89         return res;
90     }
91
92     /**
93      * Since PSK will be used directly while PIN based ownership transfer,
94      * Credential should not be saved into SVR.
95      * For this reason, We will use a temporary get_psk_info callback to random PIN OxM.
96      */
97     if(CA_STATUS_OK != CARegisterDTLSCredentialsHandler(GetDtlsPskForRandomPinOxm))
98     {
99         OIC_LOG(ERROR, TAG, "Failed to register DTLS credentials handler for random PIN OxM.");
100         res = OC_STACK_ERROR;
101     }
102
103     //Set the device id to derive temporal PSK
104     SetUuidForRandomPinOxm(&(otmCtx->selectedDeviceInfo->doxm->deviceID));
105
106     return res;
107 }
108
109 OCStackResult CreateSecureSessionRandomPinCallback(OTMContext_t* otmCtx)
110 {
111     OIC_LOG(INFO, TAG, "IN CreateSecureSessionRandomPinCallbak");
112
113     if (!otmCtx || !otmCtx->selectedDeviceInfo)
114     {
115         return OC_STACK_INVALID_PARAM;
116     }
117
118     CAResult_t caresult = CAEnableAnonECDHCipherSuite(false);
119     if (CA_STATUS_OK != caresult)
120     {
121         OIC_LOG_V(ERROR, TAG, "Unable to disable anon cipher suite");
122         return OC_STACK_ERROR;
123     }
124     OIC_LOG(INFO, TAG, "Anonymous cipher suite disabled.");
125
126     caresult  = CASelectCipherSuite(TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256);
127     if (CA_STATUS_OK != caresult)
128     {
129         OIC_LOG_V(ERROR, TAG, "Failed to select TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256");
130         return OC_STACK_ERROR;
131     }
132     OIC_LOG(INFO, TAG, "TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA_256 cipher suite selected.");
133
134     OCProvisionDev_t* selDevInfo = otmCtx->selectedDeviceInfo;
135     CAEndpoint_t *endpoint = (CAEndpoint_t *)OICCalloc(1, sizeof (CAEndpoint_t));
136     if (NULL == endpoint)
137     {
138         return OC_STACK_NO_MEMORY;
139     }
140     memcpy(endpoint,&selDevInfo->endpoint,sizeof(CAEndpoint_t));
141     endpoint->port = selDevInfo->securePort;
142     caresult = CAInitiateHandshake(endpoint);
143     OICFree(endpoint);
144     if (CA_STATUS_OK != caresult)
145     {
146         OIC_LOG_V(ERROR, TAG, "DTLS handshake failure.");
147         return OC_STACK_ERROR;
148     }
149
150     OIC_LOG(INFO, TAG, "OUT CreateSecureSessionRandomPinCallbak");
151
152     return OC_STACK_OK;
153 }