Remove unused openssl-devel dependency
[platform/upstream/iotivity.git] / resource / csdk / security / src / oxmverifycommon.c
1 //******************************************************************
2 //
3 // Copyright 2016 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 "ocstack.h"
22 #include "logger.h"
23 #include "base64.h"
24 #include "securevirtualresourcetypes.h"
25 #include "srmresourcestrings.h"
26 #include "cainterface.h"
27 #include "oxmverifycommon.h"
28 #include "octhread.h"
29
30 #define TAG "OIC_VERIFY_COMMON"
31
32 static VerifyOptionBitmask_t gVerifyOption = (DISPLAY_NUM | USER_CONFIRM);
33
34 static DisplayNumContext_t gDisplayNumContext = { .callback = NULL, .context = NULL };
35 static UserConfirmContext_t gUserConfirmContext = { .callback = NULL, .context = NULL };
36 static UserAsyncConfirmContext_t gUserAsyncConfirmContext = { .callback = NULL, .context = NULL, .userConfirm = false, .userConfirmResult = false };
37 static InputStateContext_t gInputStateContext = { .callback = NULL, .context = NULL };
38 extern oc_cond g_condWait;
39
40 void SetDisplayNumCB(void * ptr, DisplayNumCallback displayNumCB)
41 {
42     OIC_LOG(DEBUG, TAG, "IN SetDisplayNumCB");
43     if (NULL == displayNumCB)
44     {
45         OIC_LOG(ERROR, TAG, "Failed to set callback for displaying mutualVerifNum");
46         return;
47     }
48     gDisplayNumContext.callback = displayNumCB;
49     gDisplayNumContext.context = ptr;
50     OIC_LOG(DEBUG, TAG, "OUT SetDisplayNumCB");
51 }
52
53 void* UnsetDisplayNumCB()
54 {
55     OIC_LOG(DEBUG, TAG, "IN UnsetDisplayNumCB");
56     void *prevctx = gDisplayNumContext.context;
57     gDisplayNumContext.callback = NULL;
58     gDisplayNumContext.context= NULL;
59     OIC_LOG(DEBUG, TAG, "OUT UnsetDisplayNumCB");
60     return prevctx;
61 }
62
63 void SetUserConfirmCB(void * ptr, UserConfirmCallback userConfirmCB)
64 {
65     OIC_LOG(DEBUG, TAG, "IN SetUserConfirmCB");
66     if (NULL == userConfirmCB)
67     {
68         OIC_LOG(ERROR, TAG, "Failed to set callback to confirm mutualVerifNum");
69         return;
70     }
71     gUserConfirmContext.callback = userConfirmCB;
72     gUserConfirmContext.context = ptr;
73     OIC_LOG(DEBUG, TAG, "OUT SetUserConfirmCB");
74 }
75
76 void* UnsetUserConfirmCB()
77 {
78     OIC_LOG(DEBUG, TAG, "IN UnsetUserConfirmCB");
79     void *prevctx = gUserConfirmContext.context;
80     gUserConfirmContext.callback = NULL;
81     gUserConfirmContext.context = NULL;
82     OIC_LOG(DEBUG, TAG, "OUT UnsetUserConfirmCB");
83     return prevctx;
84 }
85
86 void SetInputStateCB(void * ptr, InputStateCallback inputStateCB)
87 {
88     OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
89     if (NULL == inputStateCB)
90     {
91         OIC_LOG(ERROR, TAG, "Failed to set callback to notify user input state");
92         return;
93     }
94     gInputStateContext.callback = inputStateCB;
95     gInputStateContext.context = ptr;
96     OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
97 }
98
99 void* UnsetInputStateCB()
100 {
101     OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
102     void *prevctx = gInputStateContext.context;
103     gInputStateContext.callback = NULL;
104     gInputStateContext.context = NULL;
105     OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
106     return prevctx;
107 }
108
109 void SetVerifyOption(VerifyOptionBitmask_t verifyOption)
110 {
111     OIC_LOG(DEBUG, TAG, "IN SetVerifyOption");
112     gVerifyOption = verifyOption;
113     OIC_LOG_V(DEBUG, TAG, "VerifyOption set to %d", (int) gVerifyOption);
114     OIC_LOG(DEBUG, TAG, "OUT SetVerifyOption");
115 }
116
117 OCStackResult VerifyOwnershipTransfer(uint8_t mutualVerifNum [MUTUAL_VERIF_NUM_LEN],
118                                     VerifyOptionBitmask_t verifyOption)
119 {
120     OIC_LOG(DEBUG, TAG, "IN VerifyOwnershipTransfer");
121     OIC_LOG_V(DEBUG, TAG, "gVerifyOption: %d", (int) gVerifyOption);
122     verifyOption = (VerifyOptionBitmask_t)(verifyOption & gVerifyOption);
123     if (verifyOption & DISPLAY_NUM)
124     {
125         if (!gDisplayNumContext.callback)
126         {
127             OIC_LOG(ERROR, TAG, "Callback for displaying verification PIN not registered");
128             return OC_STACK_ERROR;
129         }
130         OIC_LOG(DEBUG, TAG, "calling displayNumCallback");
131         if (OC_STACK_OK != gDisplayNumContext.callback(gDisplayNumContext.context, mutualVerifNum))
132         {
133             OIC_LOG(ERROR, TAG, "Failed to display verification PIN");
134             return OC_STACK_ERROR;
135         }
136     }
137     if (verifyOption & USER_CONFIRM)
138     {
139         if (!gUserConfirmContext.callback)
140         {
141             OIC_LOG(ERROR, TAG, "Callback to get user confirmation not registered");
142             return OC_STACK_ERROR;
143         }
144         OIC_LOG(DEBUG, TAG, "calling userConfirmCallback");
145         if (OC_STACK_OK != gUserConfirmContext.callback(gUserConfirmContext.context))
146         {
147             OIC_LOG(ERROR, TAG, "Failed to get user confirmation");
148             return OC_STACK_USER_DENIED_REQ;
149         }
150     }
151     OIC_LOG(DEBUG, TAG, "OUT VerifyOwnershipTransfer");
152     return OC_STACK_OK;
153 }
154
155 OCStackResult VerifyUserConfirm()
156 {
157     OIC_LOG(DEBUG, TAG, "IN VerifyUserConfirm");
158     OIC_LOG_V(DEBUG, TAG, "gVerifyOption: %d", (int) gVerifyOption);
159     if (gVerifyOption)
160     {
161         if (!gUserAsyncConfirmContext.callback)
162         {
163             OIC_LOG(ERROR, TAG, "Callback to get user confirmation not registered");
164             return OC_STACK_ERROR;
165         }
166         OIC_LOG(DEBUG, TAG, "calling userAsyncConfirmCallback");
167         gUserAsyncConfirmContext.callback(&gUserAsyncConfirmContext);
168     }
169     OIC_LOG(DEBUG, TAG, "OUT VerifyUserConfirm");
170     return OC_STACK_OK;
171 }
172
173 void GetAsyncVerifyUserResult(bool * result, bool * confirm)
174 {
175     *result = gUserAsyncConfirmContext.userConfirmResult;
176     *confirm = gUserAsyncConfirmContext.userConfirm;
177     return;
178 }
179
180 void SetAsyncUserConfirmCB(void * ptr, UserConfirmCallback userConfirmCB)
181 {
182     OIC_LOG(DEBUG, TAG, "IN SetAsyncUserConfirmCB");
183     if (NULL == userConfirmCB)
184     {
185         OIC_LOG(ERROR, TAG, "Failed to set callback to confirm mutualVerifNum");
186         return;
187     }
188     gUserAsyncConfirmContext.callback = userConfirmCB;
189     gUserAsyncConfirmContext.context = ptr;
190     OIC_LOG(DEBUG, TAG, "OUT SetAsyncUserConfirmCB");
191 }
192
193 void* UnsetAsyncUserConfirmCB()
194 {
195     OIC_LOG(DEBUG, TAG, "IN UnsetAsyncUserConfirmCB");
196     void *prevctx = gUserAsyncConfirmContext.context;
197     gUserAsyncConfirmContext.callback = NULL;
198     gUserAsyncConfirmContext.context = NULL;
199     OIC_LOG(DEBUG, TAG, "OUT UnsetAsyncUserConfirmCB");
200     return prevctx;
201 }
202
203 OCStackResult SendUserConfirm(bool confirmed)
204 {
205     gUserAsyncConfirmContext.userConfirmResult = confirmed;
206     gUserAsyncConfirmContext.userConfirm = true;
207     oc_cond_signal(g_condWait);
208     return OC_STACK_OK;
209 }
210
211 OCStackResult NotifyInputState(void)
212 {
213     OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
214
215     if (!gInputStateContext.callback)
216     {
217         OIC_LOG(INFO, TAG, "Callback for notifying user input state not registered");
218         return OC_STACK_OK;
219     }
220     OIC_LOG(INFO, TAG, "calling inputStateCallback");
221     if (OC_STACK_OK != gInputStateContext.callback(gInputStateContext.context))
222     {
223         OIC_LOG(ERROR, TAG, "Failed to notify user input state");
224         return OC_STACK_ERROR;
225     }
226     OIC_LOG_V(DEBUG, TAG, "OUT %s", __func__);
227     return OC_STACK_OK;
228 }
229