Merge "Merge branch 'security-basecamp' into master"
[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 "ocsecurityconfig.h"
25 #include "securevirtualresourcetypes.h"
26 #include "doxmresource.h"
27 #include "credresource.h"
28 #include "cacommon.h"
29 #include "cainterface.h"
30 #include "ocrandom.h"
31 #include "oic_malloc.h"
32 #include "logger.h"
33 #include "pbkdf2.h"
34 #include "global.h"
35 #include "base64.h"
36 #include "oxmrandompin.h"
37 #include "ownershiptransfermanager.h"
38 #include "pinoxmcommon.h"
39
40 #define TAG "OXM_RandomPIN"
41
42 char* CreatePinBasedSelectOxmPayload(OTMContext_t* otmCtx)
43 {
44     if(!otmCtx || !otmCtx->selectedDeviceInfo)
45     {
46         return NULL;
47     }
48
49     otmCtx->selectedDeviceInfo->doxm->oxmSel = OIC_RANDOM_DEVICE_PIN;
50
51     OicUuid_t uuidPT = {.id={0}};
52     if (OC_STACK_OK != GetDoxmDeviceID(&uuidPT))
53     {
54         OC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
55         return NULL;
56     }
57     memcpy(otmCtx->selectedDeviceInfo->doxm->owner.id, uuidPT.id, UUID_LENGTH);
58
59     return BinToDoxmJSON(otmCtx->selectedDeviceInfo->doxm);
60 }
61
62 char* CreatePinBasedOwnerTransferPayload(OTMContext_t* otmCtx)
63 {
64     if(!otmCtx || !otmCtx->selectedDeviceInfo)
65     {
66         return NULL;
67     }
68
69     OicUuid_t uuidPT = {.id={0}};
70
71     if (OC_STACK_OK != GetDoxmDeviceID(&uuidPT))
72     {
73         OC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
74         return NULL;
75     }
76     memcpy(otmCtx->selectedDeviceInfo->doxm->owner.id, uuidPT.id , UUID_LENGTH);
77     otmCtx->selectedDeviceInfo->doxm->owned = true;
78
79     return BinToDoxmJSON(otmCtx->selectedDeviceInfo->doxm);
80 }
81
82 OCStackResult InputPinCodeCallback(OTMContext_t* otmCtx)
83 {
84     if(!otmCtx || !otmCtx->selectedDeviceInfo)
85     {
86         return OC_STACK_INVALID_PARAM;
87     }
88
89     uint8_t pinData[OXM_RANDOM_PIN_SIZE + 1];
90
91     OCStackResult res = InputPin((char*)pinData, OXM_RANDOM_PIN_SIZE + 1);
92     if(OC_STACK_OK != res)
93     {
94         OC_LOG(ERROR, TAG, "Failed to input PIN");
95         return res;
96     }
97
98     OicUuid_t deviceUUID = {.id={0}};
99     if (OC_STACK_OK != GetDoxmDeviceID(&deviceUUID))
100     {
101         OC_LOG(ERROR, TAG, "Error while retrieving provisioning tool's device ID");
102         return OC_STACK_ERROR;
103     }
104
105     res = AddTmpPskWithPIN(&otmCtx->selectedDeviceInfo->doxm->deviceID,
106                            SYMMETRIC_PAIR_WISE_KEY,
107                            (char*)pinData, OXM_RANDOM_PIN_SIZE,
108                            1, &deviceUUID, &otmCtx->tempCredId);
109     if(res != OC_STACK_OK)
110     {
111         OC_LOG_V(ERROR, TAG, "Failed to save the temporal PSK : %d", res);
112     }
113
114     return res;
115 }
116
117 OCStackResult CreateSecureSessionRandomPinCallbak(OTMContext_t* otmCtx)
118 {
119     OC_LOG(INFO, TAG, "IN CreateSecureSessionRandomPinCallbak");
120
121     if(!otmCtx || !otmCtx->selectedDeviceInfo)
122     {
123         return OC_STACK_INVALID_PARAM;
124     }
125
126     OCProvisionDev_t* selDevInfo = otmCtx->selectedDeviceInfo;
127     CAEndpoint_t *endpoint = (CAEndpoint_t *)OICCalloc(1, sizeof (CAEndpoint_t));
128     if(NULL == endpoint)
129     {
130         return OC_STACK_NO_MEMORY;
131     }
132     memcpy(endpoint,&selDevInfo->endpoint,sizeof(CAEndpoint_t));
133     endpoint->port = selDevInfo->securePort;
134     CAResult_t caresult = CAInitiateHandshake(endpoint);
135     OICFree(endpoint);
136     if (CA_STATUS_OK != caresult)
137     {
138         OC_LOG_V(ERROR, TAG, "DTLS handshake failure.");
139         return OC_STACK_ERROR;
140     }
141
142     OC_LOG(INFO, TAG, "OUT CreateSecureSessionRandomPinCallbak");
143
144     return OC_STACK_OK;
145 }