52e411fa1a98176f4eb48e9c52811ec8ddd41d58
[platform/upstream/iotivity.git] / resource / csdk / security / include / pinoxmcommon.h
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 #ifndef PIN_CALLBACK_DEF_H_
22 #define PIN_CALLBACK_DEF_H_
23
24 #include "securevirtualresourcetypes.h"
25 #include "casecurityinterface.h"
26
27 #ifdef __cplusplus
28  extern "C" {
29 #endif // __cplusplus
30
31 #define OXM_RANDOM_PIN_DEFAULT_SIZE (8)
32 #define OXM_RANDOM_PIN_DEFAULT_PIN_TYPE (NUM_PIN | LOWERCASE_CHAR_PIN | UPPERCASE_CHAR_PIN)
33 #define OXM_RANDOM_PIN_MIN_SIZE (4)
34 #define OXM_RANDOM_PIN_MAX_SIZE (32)
35 #define OXM_PRECONFIG_PIN_MAX_SIZE (OXM_RANDOM_PIN_MAX_SIZE)
36
37 /** Number of PIN type */
38 #define OXM_PIN_TYPE_COUNT 3
39
40 /**
41  * PIN type definition.
42  * This type supports multiple bit set.
43  * e.g.) NUM_PIN | UPPERCASE_CHAR_PIN
44  */
45 typedef enum OicSecPinType{
46     NUM_PIN            = (0x1 << 0),    //Numeric PIN
47     UPPERCASE_CHAR_PIN = (0x1 << 1),    //uppercase character PIN
48     LOWERCASE_CHAR_PIN = (0x1 << 2)     //lowercase character PIN
49 }OicSecPinType_t;
50
51 /**
52  * Function pointer to print pin code.
53  */
54 typedef void (*GeneratePinCallback)(char* pinData, size_t pinSize);
55
56 /**
57  * Function pointer to input pin code.
58  */
59 typedef void (*InputPinCallback)(char* pinBuf, size_t bufSize);
60
61 /**
62  * Function to setting generate PIN callback from user.
63  *
64  * @param pinCB implementation of generate PIN callback.
65  */
66 void SetGeneratePinCB(GeneratePinCallback pinCB);
67
68 /**
69  * Function to setting input PIN callback from user.
70  *
71  * @param pinCB implementation of input PIN callback.
72  */
73 void SetInputPinCB(InputPinCallback pinCB);
74
75 /**
76  * Function to unset the input PIN callback.
77  * NOTE : Do not call this function while PIN based ownership transfer.
78  */
79 void UnsetInputPinCB();
80
81 /**
82  * Function to unset the PIN generation callback.
83  * NOTE : Do not call this function while PIN based ownership transfer.
84  */
85 void UnsetGeneratePinCB();
86
87 /**
88  * Function to generate random PIN.
89  * This function will send generated PIN to user via callback.
90  *
91  * @param pinBuffer is the reference to the buffer to store the generated PIN data.
92  * @param bufferSize is the size of buffer.
93  *
94  * @return ::OC_STACK_OK in case of success or other value in case of error.
95  */
96 OCStackResult GeneratePin(char* pinBuffer, size_t bufferSize);
97
98 /**
99  * Function to input PIN callback via input callback.
100  *
101  * @param[in,out] pinBuffer is the reference to the buffer to store the inputed PIN data.
102  * @param[in] bufferSize is the size of buffer.
103  *
104  * @return ::OC_STACK_OK in case of success or other value in ccase of error.
105  */
106 OCStackResult InputPin(char* pinBuffer, size_t bufferSize);
107
108 #ifdef _ENABLE_MULTIPLE_OWNER_
109 /**
110  * Function to save the Pre-configured PIN.
111  *
112  * @param[in] pinBuffer PIN data
113  * @param[in] pinLength byte length of PIN
114  *
115  * @return ::OC_STACK_SUCCESS in case of success or other value in ccase of error.
116  */
117 OCStackResult SetPreconfigPin(const char *pinBuffer, size_t pinLength);
118 #endif
119
120 /**
121  * Function to setting the policy for random PIN generation
122  *
123  * @param[in] pinSize Byte length of random PIN
124  * @param[in] pinType Type of random PIN (ref OicSecPinType)
125  *
126  * @return ::OC_STACK_OK in case of success or other value in case of error.
127  */
128 OCStackResult SetRandomPinPolicy(size_t pinSize, OicSecPinType_t pinType);
129
130 #ifdef __WITH_DTLS__
131
132 /**
133  * This function is used by OTM and SRM to
134  * register device UUID is required to derive the temporal PSK.
135  */
136 void SetUuidForPinBasedOxm(const OicUuid_t* uuid);
137
138 /**
139  * This internal callback is used while Random PIN based OTM.
140  * This callback will be used to establish a temporary secure session according to
141  * TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256.
142  *
143  * @param[in]  type type of PSK data required by tinyDTLS layer during DTLS handshake.
144  * @param[in]  UNUSED1 UNUSED.
145  * @param[in]  UNUSED2 UNUSED.
146  * @param[out] result  Must be filled with the requested information.
147  * @param[in]  result_length  Maximum size of @p result.
148  *
149  * @return The number of bytes written to @p result or a value
150  *         less than zero on error.
151  */
152 int32_t GetDtlsPskForRandomPinOxm( CADtlsPskCredType_t type,
153               const unsigned char *UNUSED1, size_t UNUSED2,
154               unsigned char *result, size_t result_length);
155
156 #ifdef _ENABLE_MULTIPLE_OWNER_
157 /**
158  * This internal callback is used while Random PIN based MOT.
159  * This callback will be used to establish a temporary secure session according to
160  * TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256.
161  *
162  * @param[in]  type type of PSK data required by tinyDTLS layer during DTLS handshake.
163  * @param[in]  UNUSED1 UNUSED.
164  * @param[in]  UNUSED2 UNUSED.
165  * @param[out] result  Must be filled with the requested information.
166  * @param[in]  result_length  Maximum size of @p result.
167  *
168  * @return The number of bytes written to @p result or a value
169  *         less than zero on error.
170  */
171 int32_t GetDtlsPskForMotRandomPinOxm( CADtlsPskCredType_t type,
172               const unsigned char *UNUSED1, size_t UNUSED2,
173               unsigned char *result, size_t result_length);
174
175
176 /**
177  * This internal callback is used while Preconfigured-PIN OTM.
178  * This callback will be used to establish a temporary secure session according to
179  * TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256.
180  *
181  * @param[in]  type type of PSK data required by tinyDTLS layer during DTLS handshake.
182  * @param[in]  UNUSED1 UNUSED.
183  * @param[in]  UNUSED2 UNUSED.
184  * @param[out] result  Must be filled with the requested information.
185  * @param[in]  result_length  Maximum size of @p result.
186  *
187  * @return The number of bytes written to @p result or a value
188  *         less than zero on error.
189  */
190 int32_t GetDtlsPskForPreconfPinOxm( CADtlsPskCredType_t type,
191               const unsigned char *UNUSED1, size_t UNUSED2,
192               unsigned char *result, size_t result_length);
193
194
195 /**
196  * This internal callback is used while Preconfigured-PIN MOT.
197  * This callback will be used to establish a temporary secure session according to
198  * TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256.
199  *
200  * @param[in]  type type of PSK data required by tinyDTLS layer during DTLS handshake.
201  * @param[in]  UNUSED1 UNUSED.
202  * @param[in]  UNUSED2 UNUSED.
203  * @param[out] result  Must be filled with the requested information.
204  * @param[in]  result_length  Maximum size of @p result.
205  *
206  * @return The number of bytes written to @p result or a value
207  *         less than zero on error.
208  */
209 int32_t GetDtlsPskForMotPreconfPinOxm( CADtlsPskCredType_t type,
210               const unsigned char *UNUSED1, size_t UNUSED2,
211               unsigned char *result, size_t result_length);
212
213 #endif //_ENABLE_MULTIPLE_OWNER_
214
215
216 /**
217  * API to derive the PSK based on PIN and new device's UUID.
218  * New device's UUID should be set through SetUuidForPinBasedOxm() API before this API is invoked.
219  *
220  * @param[out] result generated PSK
221  *
222  * @return 0 for success, otherwise error.
223  */
224 int DerivePSKUsingPIN(uint8_t* result);
225
226 #endif //__WITH_DTLS__
227
228 #ifdef __cplusplus
229 }
230 #endif
231
232 #endif //PIN_CALLBACK_DEF_H_