21dc53695f9143e790dad71ef4a0817e0ba0c760
[platform/upstream/iotivity.git] / resource / csdk / connectivity / test / ssladapter_test.cpp
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 "gtest/gtest.h"
22 #include "time.h"
23
24 #define CAcloseSslConnection CAcloseSslConnectionTest
25 #define CAdecryptSsl CAdecryptSslTest
26 #define CAdeinitSslAdapter CAdeinitSslAdapterTest
27 #define CAencryptSsl CAencryptSslTest
28 #define CAinitSslAdapter CAinitSslAdapterTest
29 #define CAinitiateSslHandshake CAinitiateSslHandshakeTest
30 #define CAsetCredentialTypesCallback CAsetCredentialTypesCallbackTest
31 #define CAsetSslAdapterCallbacks CAsetSslAdapterCallbacksTest
32 #define CAsetSslHandshakeCallback CAsetSslHandshakeCallbackTest
33 #define CAsetTlsCipherSuite CAsetTlsCipherSuiteTest
34 #define CAsslGenerateOwnerPsk CAsslGenerateOwnerPskTest
35
36 #include "../src/adapter_util/ca_adapter_net_ssl.c"
37
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
41 #ifdef HAVE_PTHREAD_H
42 #include <pthread.h>
43 #endif
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #ifdef HAVE_SYS_TYPES_H
48 #include <sys/types.h>
49 #endif
50 #ifdef HAVE_SYS_SOCKET_H
51 #include <sys/socket.h>
52 #endif
53 #ifdef HAVE_NETINET_IN_H
54 #include <netinet/in.h>
55 #endif
56 #ifdef HAVE_NETDB_H
57 #include <netdb.h>
58 #endif
59
60 #if defined(_WIN32)
61 #include "../../../../c_common/windows/include/pthread_create.h"
62 #endif
63 #ifdef HAVE_WINDOWS_H
64 #include <windows.h>
65 /** @todo stop-gap for naming issue. Windows.h does not like us to use ERROR */
66 #ifdef ERROR
67 #undef ERROR
68 #endif //ERROR
69 #endif //HAVE_WINDOWS_H
70 #include "platform_features.h"
71 #include "logger.h"
72
73 #define MBED_TLS_DEBUG_LEVEL (4) // Verbose
74
75 #define SEED "PREDICTED_SEED"
76 #define dummyHandler 0xF123
77
78 #define SERVER_PORT 4433
79 #define SERVER_NAME "localhost"
80 #define GET_REQUEST "GET / HTTP/1.0\r\n\r\n"
81
82 /* **************************
83  *
84  *
85  * Common routines
86  *
87  *
88  * *************************/
89
90 unsigned char serverCert[] = {
91     0x30, 0x82, 0x02, 0x39, 0x30, 0x82, 0x01, 0xdf, 0x02, 0x01, 0x01, 0x30, 0x0a, 0x06, 0x08, 0x2a,
92     0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x30, 0x7c, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55,
93     0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c,
94     0x09, 0x53, 0x6f, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x74, 0x65, 0x31, 0x11, 0x30, 0x0f, 0x06, 0x03,
95     0x55, 0x04, 0x07, 0x0c, 0x08, 0x53, 0x6f, 0x6d, 0x65, 0x63, 0x69, 0x74, 0x79, 0x31, 0x0b, 0x30,
96     0x09, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x02, 0x42, 0x42, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03,
97     0x55, 0x04, 0x0b, 0x0c, 0x0d, 0x53, 0x65, 0x71, 0x75, 0x72, 0x69, 0x74, 0x79, 0x20, 0x50, 0x61,
98     0x72, 0x74, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x02, 0x6f, 0x62, 0x31,
99     0x14, 0x30, 0x12, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x05,
100     0x6f, 0x62, 0x40, 0x62, 0x62, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x35, 0x31,
101     0x33, 0x31, 0x31, 0x31, 0x37, 0x5a, 0x17, 0x0d, 0x31, 0x39, 0x30, 0x35, 0x31, 0x32, 0x31, 0x33,
102     0x31, 0x31, 0x31, 0x37, 0x5a, 0x30, 0x81, 0xd4, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
103     0x06, 0x13, 0x02, 0x55, 0x41, 0x31, 0x0c, 0x30, 0x0a, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x03,
104     0x41, 0x73, 0x64, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x06, 0x47, 0x6f,
105     0x74, 0x68, 0x61, 0x6d, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x02, 0x5a,
106     0x5a, 0x31, 0x11, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x08, 0x42, 0x65, 0x61, 0x6d,
107     0x54, 0x65, 0x61, 0x6d, 0x31, 0x1c, 0x30, 0x1a, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
108     0x01, 0x09, 0x01, 0x16, 0x0d, 0x72, 0x61, 0x69, 0x6c, 0x40, 0x6d, 0x61, 0x69, 0x6c, 0x2e, 0x63,
109     0x6f, 0x6d, 0x31, 0x32, 0x30, 0x30, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x29, 0x75, 0x75, 0x69,
110     0x64, 0x3a, 0x33, 0x32, 0x33, 0x32, 0x33, 0x32, 0x33, 0x32, 0x2d, 0x33, 0x32, 0x33, 0x32, 0x2d,
111     0x33, 0x32, 0x33, 0x32, 0x2d, 0x33, 0x32, 0x33, 0x32, 0x2d, 0x33, 0x32, 0x33, 0x32, 0x33, 0x32,
112     0x33, 0x32, 0x33, 0x32, 0x33, 0x32, 0x31, 0x34, 0x30, 0x32, 0x06, 0x03, 0x55, 0x1d, 0x11, 0x0c,
113     0x2b, 0x75, 0x73, 0x65, 0x72, 0x69, 0x64, 0x3a, 0x36, 0x37, 0x36, 0x37, 0x36, 0x37, 0x36, 0x37,
114     0x2d, 0x36, 0x37, 0x36, 0x37, 0x2d, 0x36, 0x37, 0x36, 0x37, 0x2d, 0x36, 0x37, 0x36, 0x37, 0x2d,
115     0x36, 0x37, 0x36, 0x37, 0x36, 0x37, 0x36, 0x37, 0x36, 0x37, 0x36, 0x37, 0x30, 0x59, 0x30, 0x13,
116     0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
117     0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0xf7, 0x13, 0x5c, 0x73, 0x72, 0xce, 0x10, 0xe5, 0x09,
118     0x97, 0x9a, 0xf8, 0xf2, 0x70, 0xa6, 0x3d, 0x89, 0xf5, 0xc5, 0xe4, 0x44, 0xe2, 0x4a, 0xb6, 0x61,
119     0xa8, 0x12, 0x8d, 0xb4, 0xdc, 0x2b, 0x47, 0x84, 0x60, 0x0c, 0x25, 0x66, 0xe9, 0xe0, 0xe5, 0xac,
120     0x22, 0xbf, 0x15, 0xdc, 0x71, 0xb1, 0x88, 0x4f, 0x16, 0xbf, 0xc2, 0x77, 0x37, 0x76, 0x3f, 0xe0,
121     0x67, 0xc6, 0x1d, 0x23, 0xfe, 0x7c, 0x8b, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
122     0x04, 0x03, 0x02, 0x03, 0x48, 0x00, 0x30, 0x45, 0x02, 0x20, 0x47, 0xcc, 0x41, 0x8a, 0x27, 0xc7,
123     0xd0, 0xaa, 0xb4, 0xab, 0x85, 0xbf, 0x09, 0x4d, 0x06, 0xd7, 0x7e, 0x0d, 0x39, 0xf9, 0x36, 0xa1,
124     0x3d, 0x96, 0x23, 0xe2, 0x24, 0x64, 0x98, 0x63, 0x21, 0xba, 0x02, 0x21, 0x00, 0xe5, 0x8f, 0x7f,
125     0xf1, 0xa6, 0x82, 0x03, 0x6a, 0x18, 0x7a, 0x54, 0xe7, 0x0e, 0x25, 0x77, 0xd8, 0x46, 0xfa, 0x96,
126     0x8a, 0x7e, 0x14, 0xc4, 0xcb, 0x21, 0x32, 0x3e, 0x89, 0xd9, 0xba, 0x8c, 0x3f
127 };
128 int serverCertLen = sizeof(serverCert);
129
130 unsigned char serverPrivateKey[] = {
131     0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x02, 0x51, 0xb5, 0x97, 0xb9, 0xe9, 0xd8, 0x8d, 0x66,
132     0x2b, 0x8a, 0xb3, 0x9c, 0x6a, 0xd2, 0xca, 0x18, 0x21, 0xb9, 0x87, 0x3d, 0xf5, 0x8e, 0xa2, 0x8d,
133     0x38, 0xf6, 0xb7, 0xd2, 0x76, 0x74, 0x99, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
134     0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xf7, 0x13, 0x5c, 0x73, 0x72, 0xce, 0x10,
135     0xe5, 0x09, 0x97, 0x9a, 0xf8, 0xf2, 0x70, 0xa6, 0x3d, 0x89, 0xf5, 0xc5, 0xe4, 0x44, 0xe2, 0x4a,
136     0xb6, 0x61, 0xa8, 0x12, 0x8d, 0xb4, 0xdc, 0x2b, 0x47, 0x84, 0x60, 0x0c, 0x25, 0x66, 0xe9, 0xe0,
137     0xe5, 0xac, 0x22, 0xbf, 0x15, 0xdc, 0x71, 0xb1, 0x88, 0x4f, 0x16, 0xbf, 0xc2, 0x77, 0x37, 0x76,
138     0x3f, 0xe0, 0x67, 0xc6, 0x1d, 0x23, 0xfe, 0x7c, 0x8b
139 };
140
141 int serverPrivateKeyLen = sizeof(serverPrivateKey);
142
143 unsigned char caCert[] = {
144     0x30, 0x82, 0x02, 0x3e, 0x30, 0x82, 0x01, 0xe5, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x09, 0x00,
145     0x87, 0xa7, 0x68, 0x01, 0x7c, 0xe9, 0xf8, 0xf0, 0x30, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce,
146     0x3d, 0x04, 0x03, 0x02, 0x30, 0x7c, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
147     0x02, 0x55, 0x53, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x09, 0x53, 0x6f,
148     0x6d, 0x65, 0x73, 0x74, 0x61, 0x74, 0x65, 0x31, 0x11, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x04, 0x07,
149     0x0c, 0x08, 0x53, 0x6f, 0x6d, 0x65, 0x63, 0x69, 0x74, 0x79, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03,
150     0x55, 0x04, 0x0a, 0x0c, 0x02, 0x42, 0x42, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x0b,
151     0x0c, 0x0d, 0x53, 0x65, 0x71, 0x75, 0x72, 0x69, 0x74, 0x79, 0x20, 0x50, 0x61, 0x72, 0x74, 0x31,
152     0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x02, 0x6f, 0x62, 0x31, 0x14, 0x30, 0x12,
153     0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x05, 0x6f, 0x62, 0x40,
154     0x62, 0x62, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x36, 0x30, 0x37, 0x32, 0x35, 0x31, 0x31, 0x31, 0x36,
155     0x31, 0x31, 0x5a, 0x17, 0x0d, 0x31, 0x39, 0x30, 0x35, 0x31, 0x35, 0x31, 0x31, 0x31, 0x36, 0x31,
156     0x31, 0x5a, 0x30, 0x7c, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
157     0x53, 0x31, 0x12, 0x30, 0x10, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x09, 0x53, 0x6f, 0x6d, 0x65,
158     0x73, 0x74, 0x61, 0x74, 0x65, 0x31, 0x11, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x08,
159     0x53, 0x6f, 0x6d, 0x65, 0x63, 0x69, 0x74, 0x79, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
160     0x0a, 0x0c, 0x02, 0x42, 0x42, 0x31, 0x16, 0x30, 0x14, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0d,
161     0x53, 0x65, 0x71, 0x75, 0x72, 0x69, 0x74, 0x79, 0x20, 0x50, 0x61, 0x72, 0x74, 0x31, 0x0b, 0x30,
162     0x09, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x02, 0x6f, 0x62, 0x31, 0x14, 0x30, 0x12, 0x06, 0x09,
163     0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x05, 0x6f, 0x62, 0x40, 0x62, 0x62,
164     0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a,
165     0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x2e, 0xcf, 0xc3, 0xfa, 0x2e,
166     0x04, 0x11, 0x62, 0x34, 0x63, 0x6f, 0xdf, 0xb6, 0x67, 0xfb, 0x5a, 0x50, 0x8c, 0x15, 0x73, 0xc9,
167     0xc1, 0x57, 0x3a, 0x9e, 0xf8, 0xf4, 0xa8, 0x0c, 0x1a, 0xe9, 0x91, 0x51, 0x9d, 0x03, 0x26, 0x48,
168     0xaa, 0x46, 0x84, 0x12, 0x06, 0x2d, 0xfc, 0x66, 0xbe, 0x41, 0xed, 0xfd, 0xcd, 0x32, 0xa3, 0x9b,
169     0x34, 0xf2, 0xaa, 0x95, 0x1f, 0x8e, 0x5d, 0x49, 0x77, 0x80, 0xc2, 0xa3, 0x50, 0x30, 0x4e, 0x30,
170     0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x1e, 0x81, 0xc3, 0x62, 0xff, 0x8c,
171     0x5a, 0x98, 0x90, 0xac, 0x2c, 0xc3, 0x65, 0xb9, 0x3f, 0x8f, 0x04, 0x55, 0xfa, 0x7c, 0x30, 0x1f,
172     0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0x1e, 0x81, 0xc3, 0x62, 0xff,
173     0x8c, 0x5a, 0x98, 0x90, 0xac, 0x2c, 0xc3, 0x65, 0xb9, 0x3f, 0x8f, 0x04, 0x55, 0xfa, 0x7c, 0x30,
174     0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xff, 0x30, 0x0a, 0x06,
175     0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x47, 0x00, 0x30, 0x44, 0x02, 0x20,
176     0x1c, 0xa1, 0x55, 0xa8, 0x04, 0xbc, 0x5b, 0x00, 0xa8, 0xac, 0x2f, 0xe6, 0xb7, 0x3c, 0x7c, 0xf3,
177     0x7e, 0x93, 0xce, 0xe0, 0xdf, 0x6e, 0x36, 0xe4, 0x36, 0x20, 0xcb, 0x36, 0x9c, 0x13, 0x3b, 0xc4,
178     0x02, 0x20, 0x7f, 0x18, 0x13, 0x7d, 0x1b, 0x8c, 0xe3, 0x5b, 0xd9, 0xac, 0x74, 0x8c, 0xc0, 0xe9,
179     0xbf, 0x1b, 0x48, 0x6f, 0xb6, 0x6a, 0x45, 0x03, 0xa6, 0x5d, 0x4d, 0x65, 0xf7, 0x96, 0xa0, 0x08,
180     0x83, 0x7c
181 };
182 int caCertLen = sizeof(caCert);
183
184 unsigned char control_server_message[] = {
185     0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30, 0x20, 0x32, 0x30, 0x30, 0x20, 0x4f, 0x4b, 0x0d,
186     0x0a, 0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x54, 0x79, 0x70, 0x65, 0x3a, 0x20, 0x74,
187     0x65, 0x78, 0x74, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x0d, 0x0a, 0x0d, 0x0a, 0x3c, 0x68, 0x32, 0x3e,
188     0x6d, 0x62, 0x65, 0x64, 0x20, 0x54, 0x4c, 0x53, 0x20, 0x54, 0x65, 0x73, 0x74, 0x20, 0x53, 0x65,
189     0x72, 0x76, 0x65, 0x72, 0x3c, 0x2f, 0x68, 0x32, 0x3e, 0x0d, 0x0a, 0x54, 0x45, 0x53, 0x54, 0x20,
190     0x4d, 0x45, 0x53, 0x53, 0x41, 0x47, 0x45, 0x0d, 0x0a, 0x3c, 0x70, 0x3e, 0x53, 0x75, 0x63, 0x63,
191     0x65, 0x73, 0x73, 0x66, 0x75, 0x6c, 0x20, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f,
192     0x6e, 0x20, 0x75, 0x73, 0x69, 0x6e, 0x67, 0x3a, 0x20, 0x54, 0x4c, 0x53, 0x2d, 0x45, 0x43, 0x44,
193     0x48, 0x45, 0x2d, 0x45, 0x43, 0x44, 0x53, 0x41, 0x2d, 0x57, 0x49, 0x54, 0x48, 0x2d, 0x41, 0x45,
194     0x53, 0x2d, 0x31, 0x32, 0x38, 0x2d, 0x43, 0x43, 0x4d, 0x2d, 0x38, 0x3c, 0x2f, 0x70, 0x3e, 0x0d,
195     0x0a
196 };
197 int control_server_message_len = sizeof(control_server_message);
198
199 unsigned char control_client_message[] = {
200     0x47, 0x45, 0x54, 0x20, 0x2f, 0x20, 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30, 0x0d, 0x0a,
201     0x0d, 0x0a
202 };
203 int control_client_message_len = sizeof(control_client_message);
204
205 unsigned char caTrustedCrl[] = {
206     0x30, 0x82, 0x03, 0x11, 0x30, 0x82, 0x01, 0xf9, 0x02, 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a,
207     0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00, 0x30, 0x49, 0x31, 0x0b, 0x30, 0x09,
208     0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
209     0x04, 0x0a, 0x13, 0x0a, 0x47, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x20, 0x49, 0x6e, 0x63, 0x31, 0x25,
210     0x30, 0x23, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x1c, 0x47, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x20,
211     0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x20, 0x41, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x69,
212     0x74, 0x79, 0x20, 0x47, 0x32, 0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x32, 0x35, 0x30, 0x31, 0x30,
213     0x30, 0x30, 0x33, 0x5a, 0x17, 0x0d, 0x31, 0x36, 0x30, 0x39, 0x30, 0x34, 0x30, 0x31, 0x30, 0x30,
214     0x30, 0x33, 0x5a, 0x30, 0x82, 0x01, 0x48, 0x30, 0x27, 0x02, 0x08, 0x40, 0x3a, 0x89, 0xa6, 0xdf,
215     0x4c, 0xfc, 0xab, 0x17, 0x0d, 0x31, 0x36, 0x30, 0x31, 0x32, 0x30, 0x31, 0x30, 0x30, 0x37, 0x34,
216     0x37, 0x5a, 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x03, 0x55, 0x1d, 0x15, 0x04, 0x03, 0x0a, 0x01, 0x03,
217     0x30, 0x27, 0x02, 0x08, 0x37, 0x7a, 0xe3, 0xb0, 0x9d, 0x40, 0xe5, 0x42, 0x17, 0x0d, 0x31, 0x35,
218     0x31, 0x30, 0x30, 0x32, 0x31, 0x30, 0x32, 0x33, 0x32, 0x32, 0x5a, 0x30, 0x0c, 0x30, 0x0a, 0x06,
219     0x03, 0x55, 0x1d, 0x15, 0x04, 0x03, 0x0a, 0x01, 0x01, 0x30, 0x27, 0x02, 0x08, 0x59, 0x14, 0xab,
220     0x0a, 0x4c, 0xf7, 0xa1, 0xff, 0x17, 0x0d, 0x31, 0x36, 0x30, 0x38, 0x31, 0x36, 0x32, 0x31, 0x34,
221     0x34, 0x31, 0x32, 0x5a, 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x03, 0x55, 0x1d, 0x15, 0x04, 0x03, 0x0a,
222     0x01, 0x01, 0x30, 0x27, 0x02, 0x08, 0x6e, 0x8f, 0xb3, 0x78, 0x8f, 0x54, 0x15, 0xbb, 0x17, 0x0d,
223     0x31, 0x36, 0x30, 0x38, 0x31, 0x36, 0x32, 0x31, 0x34, 0x34, 0x34, 0x30, 0x5a, 0x30, 0x0c, 0x30,
224     0x0a, 0x06, 0x03, 0x55, 0x1d, 0x15, 0x04, 0x03, 0x0a, 0x01, 0x01, 0x30, 0x27, 0x02, 0x08, 0x23,
225     0x8a, 0x26, 0xd7, 0x52, 0xe4, 0xc2, 0x59, 0x17, 0x0d, 0x31, 0x36, 0x30, 0x34, 0x31, 0x35, 0x31,
226     0x35, 0x30, 0x30, 0x32, 0x31, 0x5a, 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x03, 0x55, 0x1d, 0x15, 0x04,
227     0x03, 0x0a, 0x01, 0x03, 0x30, 0x27, 0x02, 0x08, 0x75, 0xdd, 0xb5, 0xf7, 0x03, 0x75, 0x0e, 0x96,
228     0x17, 0x0d, 0x31, 0x36, 0x30, 0x36, 0x30, 0x38, 0x30, 0x39, 0x35, 0x32, 0x34, 0x37, 0x5a, 0x30,
229     0x0c, 0x30, 0x0a, 0x06, 0x03, 0x55, 0x1d, 0x15, 0x04, 0x03, 0x0a, 0x01, 0x05, 0x30, 0x27, 0x02,
230     0x08, 0x79, 0x61, 0xde, 0x97, 0x96, 0x20, 0x31, 0x87, 0x17, 0x0d, 0x31, 0x36, 0x30, 0x36, 0x31,
231     0x35, 0x31, 0x33, 0x33, 0x34, 0x31, 0x36, 0x5a, 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x03, 0x55, 0x1d,
232     0x15, 0x04, 0x03, 0x0a, 0x01, 0x05, 0x30, 0x27, 0x02, 0x08, 0x01, 0x48, 0x71, 0x3e, 0xaf, 0xd9,
233     0x26, 0x62, 0x17, 0x0d, 0x31, 0x36, 0x30, 0x34, 0x31, 0x35, 0x31, 0x35, 0x30, 0x34, 0x34, 0x34,
234     0x5a, 0x30, 0x0c, 0x30, 0x0a, 0x06, 0x03, 0x55, 0x1d, 0x15, 0x04, 0x03, 0x0a, 0x01, 0x03, 0xa0,
235     0x30, 0x30, 0x2e, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14,
236     0x4a, 0xdd, 0x06, 0x16, 0x1b, 0xbc, 0xf6, 0x68, 0xb5, 0x76, 0xf5, 0x81, 0xb6, 0xbb, 0x62, 0x1a,
237     0xba, 0x5a, 0x81, 0x2f, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x1d, 0x14, 0x04, 0x04, 0x02, 0x02, 0x04,
238     0xd1, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00,
239     0x03, 0x82, 0x01, 0x01, 0x00, 0x49, 0x3d, 0xd3, 0xc0, 0x71, 0x14, 0xf8, 0x55, 0xec, 0x53, 0xeb,
240     0xd9, 0xcd, 0x92, 0xe9, 0x87, 0xd9, 0x85, 0x6f, 0x67, 0x33, 0x8f, 0x72, 0x5f, 0x98, 0x46, 0x6d,
241     0x84, 0xb5, 0x4f, 0xc0, 0x85, 0x11, 0x48, 0xb0, 0x11, 0x46, 0x27, 0xb7, 0x4c, 0x7a, 0x4c, 0xf2,
242     0x6e, 0x24, 0x99, 0xa7, 0xe1, 0xb1, 0x48, 0x86, 0x84, 0x5d, 0xab, 0x27, 0x3a, 0xc4, 0xc3, 0xe5,
243     0x41, 0xa9, 0x4c, 0x30, 0x96, 0x74, 0x8b, 0xca, 0x79, 0x69, 0x8d, 0x9d, 0x9b, 0xea, 0xe3, 0x0e,
244     0x1a, 0x8c, 0xa2, 0x85, 0x1c, 0x87, 0x5d, 0x3b, 0x59, 0x93, 0x54, 0x3b, 0xba, 0x9b, 0x73, 0x91,
245     0x20, 0x72, 0xa6, 0x82, 0x72, 0x0a, 0xb2, 0xe1, 0xc8, 0xfe, 0xcd, 0x18, 0x38, 0x51, 0x36, 0xcf,
246     0x92, 0x0d, 0x35, 0x3d, 0x53, 0xfb, 0xed, 0x0d, 0x53, 0x1d, 0xa3, 0xa1, 0xe8, 0x49, 0x3c, 0x00,
247     0x62, 0xf9, 0xa7, 0x5b, 0x8b, 0x2f, 0x19, 0x84, 0x9d, 0x58, 0x96, 0x69, 0x3b, 0xd5, 0x1c, 0x14,
248     0x15, 0xb3, 0x2f, 0xb0, 0x4e, 0xb6, 0x81, 0xe9, 0x89, 0x15, 0xbf, 0xac, 0xca, 0xc6, 0xf7, 0x53,
249     0xfd, 0x3e, 0xde, 0x83, 0xb8, 0x3f, 0x66, 0xb2, 0x59, 0xaa, 0x20, 0x6b, 0x3c, 0xb0, 0x22, 0x05,
250     0x90, 0x30, 0xdc, 0x59, 0x81, 0x60, 0xbf, 0xd4, 0x77, 0xf4, 0xd7, 0xb6, 0xb4, 0xe5, 0x4c, 0xcf,
251     0xb9, 0x27, 0x9d, 0xe0, 0xd1, 0x5c, 0xea, 0x31, 0xc1, 0x54, 0x42, 0x4a, 0xab, 0xff, 0x33, 0xc6,
252     0x9a, 0xfb, 0xac, 0x47, 0x92, 0xe3, 0xfe, 0x35, 0xaa, 0xbc, 0xa6, 0x2f, 0x6c, 0xd3, 0xf0, 0xe7,
253     0xc4, 0x02, 0x1e, 0x67, 0x94, 0x47, 0x27, 0x8e, 0xbb, 0x91, 0xb7, 0xab, 0x95, 0x04, 0xdd, 0x89,
254     0x41, 0x4e, 0x87, 0xfd, 0xf5, 0x8a, 0x1b, 0x24, 0xf4, 0x3c, 0x41, 0xa2, 0xb0, 0xda, 0x27, 0xbb,
255     0x8b, 0x5b, 0xd2, 0xf9, 0x6a
256 };
257
258 static void error(const char *msg)
259 {
260     perror(msg);
261     exit(0);
262 }
263
264 static int sockfd, newsockfd;
265
266 static void socketConnect()
267 {
268     int portno;
269     struct sockaddr_in serv_addr;
270     struct hostent *server;
271
272     portno = SERVER_PORT;
273     sockfd = socket(AF_INET, SOCK_STREAM, 0);
274     if (sockfd < 0)
275         error("ERROR opening socket");
276     server = gethostbyname(SERVER_NAME);
277     if (server == NULL) {
278         fprintf(stderr,"ERROR, no such host\n");
279         exit(0);
280     }
281     //memset((char *) &serv_addr, sizeof(serv_addr));
282     memset((void*)&serv_addr, 0, sizeof(serv_addr));
283     serv_addr.sin_family = AF_INET;
284     memcpy((char *)server->h_addr,
285          (char *)&serv_addr.sin_addr.s_addr,
286          server->h_length);
287     serv_addr.sin_port = htons(portno);
288     if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
289         error("ERROR connecting");
290 }
291
292 static void CATCPPacketSendCB(CAEndpoint_t *, const void *buf, uint32_t buflen)
293 {
294     int n;
295     n = write(sockfd, buf, buflen);
296     if (n < 0)
297          error("ERROR writing to socket");
298 }
299
300 char msg[256] = {0}; int msglen = 0;
301 static void CATCPPacketReceivedCB(const CASecureEndpoint_t *, const void *data, uint32_t dataLength)
302 {
303     memcpy(msg, data, dataLength);
304     msglen = dataLength;
305 }
306
307 static void PacketReceive(unsigned char *data, int * datalen)
308 {
309     int n;
310     char buffer[2048] = {'\0'};
311     n = read(sockfd, buffer, 5);
312     if ((buffer[0] == 0x16 || buffer[0] == 0x14 || buffer[0] == 0x17 || buffer[0] == 0x15)
313         && buffer[1] == 0x03 && buffer[2] == 0x03)
314     {
315         int tlslen = (unsigned char)buffer[3] * 0x100 + (unsigned char)buffer[4];
316         n = read(sockfd, buffer + 5, tlslen);
317     }
318
319     if (n < 0)
320          error("ERROR reading from socket");
321
322     *datalen = n + 5;
323     memcpy(data, buffer, *datalen);
324 }
325
326 static void socketClose()
327 {
328     close(sockfd);
329 }
330
331 static void infoCallback_that_loads_x509(PkiInfo_t * inf)
332 {
333     inf->crt.data = (uint8_t*)serverCert;
334     inf->crt.len = sizeof(serverCert);
335     inf->key.data = (uint8_t*)serverPrivateKey;
336     inf->key.len = sizeof(serverPrivateKey);
337     inf->ca.data = (uint8_t*)caCert;
338     inf->ca.len = sizeof(caCert);
339     inf->crl.data = (uint8_t*)caTrustedCrl;
340     inf->crl.len = sizeof(caTrustedCrl);
341 }
342
343 static void socketOpen_server()
344 {
345     int portno;
346     struct sockaddr_in serv_addr, cli_addr;
347     socklen_t clilen;
348
349     portno = SERVER_PORT;
350     sockfd = socket(AF_INET, SOCK_STREAM, 0);
351     if (sockfd < 0)
352         error("\nERROR opening socket");
353     //bzero((char *) &serv_addr, sizeof(serv_addr));
354     memset((void*)&serv_addr, 0, sizeof(serv_addr));
355     serv_addr.sin_family = AF_INET;
356     serv_addr.sin_addr.s_addr = INADDR_ANY;
357     serv_addr.sin_port = htons(portno);
358     if (bind(sockfd, (struct sockaddr *) &serv_addr,
359               sizeof(serv_addr)) < 0)
360               error("\nERROR on binding");
361     listen(sockfd,5);
362     clilen = sizeof(cli_addr);
363     newsockfd = accept(sockfd,
364                  (struct sockaddr *) &cli_addr,
365                  &clilen);
366     if (newsockfd < 0)
367           error("\nERROR on accept");
368 }
369
370 static void CATCPPacketSendCB_server(CAEndpoint_t *, const void *buf, uint32_t buflen)
371 {
372     int n;
373     n = write(newsockfd,buf,buflen);
374     if (n < 0)
375          error("ERROR writing to socket");
376 }
377
378 static void CATCPPacketReceivedCB_server(const CASecureEndpoint_t *, const void *data, uint32_t dataLength)
379 {
380     memcpy(msg, data, dataLength);
381     msglen = dataLength;
382 }
383 static void PacketReceive_server(unsigned char *data, int * datalen)
384 {
385     int n;
386     char buffer[2048] = {'\0'};
387     n = read(newsockfd, buffer, 5);
388
389     if (buffer[0] == 0x16 || buffer[0] == 0x14 || buffer[0] == 0x17 || buffer[0] == 0x15)
390     {
391         int tlslen = (unsigned char)buffer[3] * 0x100 + (unsigned char)buffer[4];
392         n = read(newsockfd, buffer + 5, tlslen);
393     }
394
395     if (n < 0)
396          error("\nERROR reading from socket");
397
398     *datalen = n + 5;
399     memcpy(data, buffer, *datalen);
400 }
401
402 static void socketClose_server()
403 {
404     close(newsockfd);
405     close(sockfd);
406 }
407
408 static void clutch(bool * list)
409 {
410     list[1] = true;
411 }
412
413 const unsigned char IDENTITY[] = ("6767676767676767");
414 const unsigned char RS_CLIENT_PSK[] = ("AAAAAAAAAAAAAAAA");
415 static int32_t GetDtlsPskCredentials( CADtlsPskCredType_t,
416               const unsigned char *, size_t,
417               unsigned char *result, size_t)
418 {
419     int32_t ret = -1;
420
421     if (NULL == result)
422     {
423         return ret;
424     }
425     memcpy(result, IDENTITY, sizeof(IDENTITY));
426     ret = sizeof(IDENTITY);
427
428     return ret;
429 }
430
431 /* **************************
432  *
433  *
434  * MbedTLS client routine
435  *
436  *
437  * *************************/
438
439 #if !defined(MBEDTLS_CONFIG_FILE)
440 #include "mbedtls/config.h"
441 #else
442 #include MBEDTLS_CONFIG_FILE
443 #endif
444
445 #if defined(MBEDTLS_PLATFORM_C)
446 #include "mbedtls/platform.h"
447 #else
448 #include <stdio.h>
449 #include <stdlib.h>
450 #define mbedtls_fprintf    fprintf
451 #define mbedtls_printf     printf
452 #endif
453
454 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) ||  \
455     !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
456     !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_RSA_C) ||         \
457     !defined(MBEDTLS_CERTS_C) || !defined(MBEDTLS_PEM_PARSE_C) || \
458     !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_X509_CRT_PARSE_C)
459 static int client( void )
460 {
461     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
462            "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
463            "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
464            "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C "
465            "not defined.\n");
466     return( 0 );
467 }
468 #else
469
470 #include "mbedtls/net.h"
471 #include "mbedtls/debug.h"
472 #include "mbedtls/ssl.h"
473 #include "mbedtls/entropy.h"
474 #include "mbedtls/ctr_drbg.h"
475 #include "mbedtls/error.h"
476 #include "mbedtls/certs.h"
477
478 #include <string.h>
479
480 #define DEBUG_LEVEL (0)
481
482 static void my_debug_client( void *ctx, int level,
483                       const char *file, int line,
484                       const char *str )
485 {
486     ((void) level);
487
488     mbedtls_fprintf( (FILE *) ctx, "%s:%04d: %s", file, line, str );
489     fflush(  (FILE *) ctx  );
490 }
491
492 static void * client(void *)
493 {
494     int ret, len;
495     mbedtls_net_context server_fd;
496     uint32_t flags;
497     unsigned char buf[1024];
498     const char *pers = "ssl_client1";
499
500     mbedtls_entropy_context entropy;
501     mbedtls_ctr_drbg_context ctr_drbg;
502     mbedtls_ssl_context ssl;
503     mbedtls_ssl_config conf;
504     mbedtls_x509_crt cacert;
505     mbedtls_x509_crt owncert;
506     mbedtls_pk_context pkey;
507
508     /*
509      * 0. Initialize the RNG and the session data
510      */
511     mbedtls_net_init( &server_fd );
512     mbedtls_ssl_init( &ssl );
513     mbedtls_ssl_config_init( &conf );
514     mbedtls_x509_crt_init( &cacert );
515     mbedtls_ctr_drbg_init( &ctr_drbg );
516     mbedtls_pk_init( &pkey );
517
518     mbedtls_printf( "\n  . Seeding the random number generator..." );
519     fflush( stdout );
520
521     mbedtls_entropy_init( &entropy );
522     if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
523                                (const unsigned char *) pers,
524                                strlen( pers ) ) ) != 0 )
525     {
526         mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", ret );
527         goto exit;
528     }
529
530     mbedtls_printf( " ok\n" );
531
532     /*
533      * 0. Initialize certificates
534      */
535     mbedtls_printf( "  . Loading the CA root certificate ..." );
536     fflush( stdout );
537     ret = mbedtls_x509_crt_parse( &cacert, (const unsigned char *) caCert, caCertLen );
538     if( ret < 0 )
539     {
540         mbedtls_printf( " failed\n  !  mbedtls_x509_crt_parse caCert returned -0x%x\n\n", -ret );
541         goto exit;
542     }
543     ret = mbedtls_x509_crt_parse( &owncert, (const unsigned char *) serverCert, serverCertLen );
544     if( ret < 0 )
545     {
546         mbedtls_printf( " failed\n  !  mbedtls_x509_crt_parse serverCert returned -0x%x\n\n", -ret );
547         goto exit;
548     }
549     ret =  mbedtls_pk_parse_key( &pkey, (const unsigned char *) serverPrivateKey,
550                                                                 serverPrivateKeyLen, NULL, 0 );
551     if( ret < 0 )
552     {
553         mbedtls_printf( " failed\n  !  mbedtls_pk_parse_key returned -0x%x\n\n", -ret );
554         goto exit;
555     }
556
557     mbedtls_printf( " ok (%d skipped)\n", ret );
558
559     /*
560      * 1. Start the connection
561      */
562     mbedtls_printf( "  . Connecting to tcp/%s/%s...", SERVER_NAME, "SERVER_PORT" );
563     fflush( stdout );
564
565     if( ( ret = mbedtls_net_connect( &server_fd, "127.0.0.1",
566                                          "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 )
567     {
568         mbedtls_printf( " failed\n  ! mbedtls_net_connect returned %d\n\n", ret );
569         goto exit;
570     }
571
572     mbedtls_printf( " ok\n" );
573
574     /*
575      * 2. Setup stuff
576      */
577     mbedtls_printf( "  . Setting up the SSL/TLS structure..." );
578     fflush( stdout );
579
580     if( ( ret = mbedtls_ssl_config_defaults( &conf,
581                     MBEDTLS_SSL_IS_CLIENT,
582                     MBEDTLS_SSL_TRANSPORT_STREAM,
583                     MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
584     {
585         mbedtls_printf( " failed\n  ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
586         goto exit;
587     }
588
589     mbedtls_printf( " ok\n" );
590
591     /* OPTIONAL is not optimal for security,
592      * but makes interop easier in this simplified example */
593     mbedtls_ssl_conf_authmode( &conf, MBEDTLS_SSL_VERIFY_OPTIONAL );
594     mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
595     mbedtls_ssl_conf_own_cert( &conf, &owncert, &pkey );
596     mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
597     mbedtls_ssl_conf_dbg( &conf, my_debug_client, stdout );
598
599     if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
600     {
601         mbedtls_printf( " failed\n  ! mbedtls_ssl_setup returned %d\n\n", ret );
602         goto exit;
603     }
604
605     if( ( ret = mbedtls_ssl_set_hostname( &ssl, "mbed TLS Server 1" ) ) != 0 )
606     {
607         mbedtls_printf( " failed\n  ! mbedtls_ssl_set_hostname returned %d\n\n", ret );
608         goto exit;
609     }
610
611     mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
612
613     /*
614      * 4. Handshake
615      */
616     mbedtls_printf( "  . Performing the SSL/TLS handshake..." );
617     fflush( stdout );
618
619     while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
620     {
621         if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
622         {
623             mbedtls_printf( " failed\n  ! mbedtls_ssl_handshake returned -0x%x\n\n", -ret );
624             goto exit;
625         }
626     }
627
628     mbedtls_printf( " ok\n" );
629
630     /*
631      * 5. Verify the server certificate
632      */
633     mbedtls_printf( "  . Verifying peer X.509 certificate..." );
634
635     /* In real life, we probably want to bail out when ret != 0 */
636     if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
637     {
638         char vrfy_buf[512];
639
640         mbedtls_printf( " failed\n" );
641
642         mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), "  ! ", flags );
643
644         mbedtls_printf( "%s\n", vrfy_buf );
645     }
646     else
647         mbedtls_printf( " ok\n" );
648
649     /*
650      * 3. Write the GET request
651      */
652     mbedtls_printf( "  > Write to server:" );
653     fflush( stdout );
654
655     len = sprintf( (char *) buf, GET_REQUEST );
656
657     while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
658     {
659         if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
660         {
661             mbedtls_printf( " failed\n  ! mbedtls_ssl_write returned %d\n\n", ret );
662             goto exit;
663         }
664     }
665
666     len = ret;
667     mbedtls_printf( " %d bytes written\n\n%s", len, (char *) buf );
668
669     /*
670      * 7. Read the HTTP response
671      */
672     mbedtls_printf( "  < Read from server:" );
673     fflush( stdout );
674
675     do
676     {
677         len = sizeof( buf ) - 1;
678         memset( buf, 0, sizeof( buf ) );
679         ret = mbedtls_ssl_read( &ssl, buf, len );
680
681         if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
682             continue;
683
684         if( ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY )
685             break;
686
687         if( ret < 0 )
688         {
689             mbedtls_printf( "failed\n  ! mbedtls_ssl_read returned %d\n\n", ret );
690             break;
691         }
692
693         if( ret == 0 )
694         {
695             mbedtls_printf( "\n\nEOF\n\n" );
696             break;
697         }
698
699         len = ret;
700         mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
701     }
702     while( 1 );
703
704     mbedtls_ssl_close_notify( &ssl );
705
706 exit:
707
708 #ifdef MBEDTLS_ERROR_C
709     if( ret != 0 )
710     {
711         char error_buf[100];
712         mbedtls_strerror( ret, error_buf, 100 );
713         mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
714     }
715 #endif
716
717     mbedtls_net_free( &server_fd );
718
719     mbedtls_x509_crt_free( &cacert );
720     mbedtls_ssl_free( &ssl );
721     mbedtls_ssl_config_free( &conf );
722     mbedtls_ctr_drbg_free( &ctr_drbg );
723     mbedtls_entropy_free( &entropy );
724
725     return NULL;
726 }
727 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
728           MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
729           MBEDTLS_CERTS_C && MBEDTLS_PEM_PARSE_C && MBEDTLS_CTR_DRBG_C &&
730           MBEDTLS_X509_CRT_PARSE_C */
731
732 /* **************************
733  *
734  *
735  * MbedTLS server routine
736  *
737  *
738  * *************************/
739
740 #if !defined(MBEDTLS_CONFIG_FILE)
741 #include "mbedtls/config.h"
742 #else
743 #include MBEDTLS_CONFIG_FILE
744 #endif
745
746 #if defined(MBEDTLS_PLATFORM_C)
747 #include "mbedtls/platform.h"
748 #else
749 #include <stdio.h>
750 #include <stdlib.h>
751 #define mbedtls_fprintf    fprintf
752 #define mbedtls_printf     printf
753 #endif
754
755 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_CERTS_C) ||    \
756     !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_SSL_TLS_C) || \
757     !defined(MBEDTLS_SSL_SRV_C) || !defined(MBEDTLS_NET_C) ||     \
758     !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_CTR_DRBG_C) ||    \
759     !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
760     !defined(MBEDTLS_PEM_PARSE_C)
761 /* int */void * server( void )
762 {
763     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_CERTS_C and/or MBEDTLS_ENTROPY_C "
764            "and/or MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_SRV_C and/or "
765            "MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
766            "MBEDTLS_CTR_DRBG_C and/or MBEDTLS_X509_CRT_PARSE_C "
767            "and/or MBEDTLS_PEM_PARSE_C not defined.\n");
768     return(/* 0 */);
769 }
770 #else
771
772 #include <stdlib.h>
773 #include <string.h>
774
775 #include "mbedtls/entropy.h"
776 #include "mbedtls/ctr_drbg.h"
777 #include "mbedtls/certs.h"
778 #include "mbedtls/x509.h"
779 #include "mbedtls/ssl.h"
780 #include "mbedtls/net.h"
781 #include "mbedtls/error.h"
782 #include "mbedtls/debug.h"
783
784 #if defined(MBEDTLS_SSL_CACHE_C)
785 #include "mbedtls/ssl_cache.h"
786 #endif
787
788 #define HTTP_RESPONSE \
789     "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
790     "<h2>mbed TLS Test Server</h2>\r\n" \
791     "TEST MESSAGE\r\n" \
792     "<p>Successful connection using: %s</p>\r\n"
793
794 #define DEBUG_LEVEL (0)
795
796 static int mbedtls_entropy_func_clutch(void *, unsigned char *output, size_t len)
797 {
798     for (uint32_t i = 0; i < len; i++) output[i] = 0x11;
799     return 0;
800 }
801
802 static void my_debug( void *ctx, int level,
803                       const char *file, int line,
804                       const char *str )
805 {
806     ((void) level);
807
808     mbedtls_fprintf((FILE *) ctx, "%s:%04d: %s", file, line, str);
809     fflush(  (FILE *) ctx  );
810 }
811
812 static void * server(void *)
813 {
814     int ret, len;
815     mbedtls_net_context listen_fd, client_fd;
816     unsigned char buf[1024];
817     const char *pers = "ssl_server";
818
819     mbedtls_entropy_context entropy;
820     mbedtls_ctr_drbg_context ctr_drbg;
821     mbedtls_ssl_context ssl;
822     mbedtls_ssl_config conf;
823     mbedtls_x509_crt srvcert;
824     mbedtls_pk_context pkey;
825 #if defined(MBEDTLS_SSL_CACHE_C)
826     mbedtls_ssl_cache_context cache;
827 #endif
828
829     mbedtls_net_init( &listen_fd );
830     mbedtls_net_init( &client_fd );
831     mbedtls_ssl_init( &ssl );
832     mbedtls_ssl_config_init( &conf );
833 #if defined(MBEDTLS_SSL_CACHE_C)
834     mbedtls_ssl_cache_init( &cache );
835 #endif
836     mbedtls_x509_crt_init( &srvcert );
837     mbedtls_pk_init( &pkey );
838     mbedtls_entropy_init( &entropy );
839     mbedtls_ctr_drbg_init( &ctr_drbg );
840
841 #if defined(MBEDTLS_DEBUG_C)
842     mbedtls_debug_set_threshold( DEBUG_LEVEL );
843 #endif
844
845     /*
846      * 1. Load the certificates and private RSA key
847      */
848     mbedtls_printf( "\n  . Loading the server cert. and key..." );
849     fflush( stdout );
850
851     /*
852      * This demonstration program uses embedded test certificates.
853      * Instead, you may want to use mbedtls_x509_crt_parse_file() to read the
854      * server and CA certificates, as well as mbedtls_pk_parse_keyfile().
855      */
856     ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) serverCert, serverCertLen );
857     if( ret != 0 )
858     {
859         mbedtls_printf( " failed\n  !  mbedtls_x509_crt_parse returned %d\n\n", ret );
860         goto exit;
861     }
862
863     ret = mbedtls_x509_crt_parse( &srvcert, (const unsigned char *) caCert, caCertLen );
864     if( ret != 0 )
865     {
866         mbedtls_printf( " failed\n  !  mbedtls_x509_crt_parse returned %d\n\n", ret );
867         goto exit;
868     }
869
870     ret =  mbedtls_pk_parse_key( &pkey, (const unsigned char *) serverPrivateKey,
871                                                                 serverPrivateKeyLen, NULL, 0 );
872     if( ret != 0 )
873     {
874         mbedtls_printf( " failed\n  !  mbedtls_pk_parse_key returned %d\n\n", ret );
875         goto exit;
876     }
877
878     mbedtls_printf( " ok\n" );
879
880     /*
881      * 2. Setup the listening TCP socket
882      */
883     mbedtls_printf( "  . Bind on https://localhost:4433/ ..." );
884     fflush( stdout );
885
886     if( ( ret = mbedtls_net_bind( &listen_fd, NULL, "4433", MBEDTLS_NET_PROTO_TCP ) ) != 0 )
887     {
888         mbedtls_printf( " failed\n  ! mbedtls_net_bind returned %d\n\n", ret );
889         goto exit;
890     }
891
892     mbedtls_printf( " ok\n" );
893
894     /*
895      * 3. Seed the RNG
896      */
897     mbedtls_printf( "  . Seeding the random number generator..." );
898     fflush( stdout );
899
900     if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func_clutch, &entropy,
901                                (const unsigned char *) pers,
902                                strlen( pers ) ) ) != 0 )
903     {
904         mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", ret );
905     }
906
907     mbedtls_printf( " ok\n" );
908
909     /*
910      * 4. Setup stuff
911      */
912     mbedtls_printf( "  . Setting up the SSL data...." );
913     fflush( stdout );
914
915     if( ( ret = mbedtls_ssl_config_defaults( &conf,
916                     MBEDTLS_SSL_IS_SERVER,
917                     MBEDTLS_SSL_TRANSPORT_STREAM,
918                     MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
919     {
920         mbedtls_printf( " failed\n  ! mbedtls_ssl_config_defaults returned %d\n\n", ret );
921         goto exit;
922     }
923
924     mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
925     mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
926
927 #if defined(MBEDTLS_SSL_CACHE_C)
928     mbedtls_ssl_conf_session_cache( &conf, &cache,
929                                    mbedtls_ssl_cache_get,
930                                    mbedtls_ssl_cache_set );
931 #endif
932
933     mbedtls_ssl_conf_ca_chain( &conf, srvcert.next, NULL );
934     if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, &pkey ) ) != 0 )
935     {
936         mbedtls_printf( " failed\n  ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
937         goto exit;
938     }
939
940     if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
941     {
942         mbedtls_printf( " failed\n  ! mbedtls_ssl_setup returned %d\n\n", ret );
943         goto exit;
944     }
945
946     mbedtls_printf( " ok\n" );
947
948 reset:
949 #ifdef MBEDTLS_ERROR_C
950     if( ret != 0 )
951     {
952         char error_buf[100];
953         mbedtls_strerror( ret, error_buf, 100 );
954         mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
955     }
956 #endif
957
958     mbedtls_net_free( &client_fd );
959
960     mbedtls_ssl_session_reset( &ssl );
961
962     /*
963      * 3. Wait until a client connects
964      */
965     mbedtls_printf( "  . Waiting for a remote connection ..." );
966     fflush( stdout );
967
968     if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
969                                     NULL, 0, NULL ) ) != 0 )
970     {
971         mbedtls_printf( " failed\n  ! mbedtls_net_accept returned %d\n\n", ret );
972         goto exit;
973     }
974
975     mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send, mbedtls_net_recv, NULL );
976
977     mbedtls_printf( " ok\n" );
978
979     /*
980      * 5. Handshake
981      */
982     mbedtls_printf( "  . Performing the SSL/TLS handshake..." );
983     fflush( stdout );
984
985     while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
986     {
987         if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
988         {
989             mbedtls_printf( " failed\n  ! mbedtls_ssl_handshake returned %d\n\n", ret );
990             goto exit;
991         }
992     }
993
994     mbedtls_printf( " ok\n" );
995
996     /*
997      * 6. Read the HTTP Request
998      */
999     mbedtls_printf( "  < Read from client:" );
1000     fflush( stdout );
1001
1002     do
1003     {
1004         len = sizeof( buf ) - 1;
1005         memset( buf, 0, sizeof( buf ) );
1006         ret = mbedtls_ssl_read( &ssl, buf, len );
1007
1008         if( ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE )
1009             continue;
1010
1011         if( ret <= 0 )
1012         {
1013             switch( ret )
1014             {
1015                 case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
1016                     mbedtls_printf( " connection was closed gracefully\n" );
1017                     break;
1018
1019                 case MBEDTLS_ERR_NET_CONN_RESET:
1020                     mbedtls_printf( " connection was reset by peer\n" );
1021                     break;
1022
1023                 default:
1024                     mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
1025                     break;
1026             }
1027
1028             break;
1029         }
1030
1031         len = ret;
1032         mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
1033
1034         if( ret > 0 )
1035             break;
1036     }
1037     while( 1 );
1038
1039     /*
1040      * 7. Write the 200 Response
1041      */
1042     mbedtls_printf( "  > Write to client:" );
1043
1044     fflush( stdout );
1045
1046     len = sprintf( (char *) buf, HTTP_RESPONSE,
1047                    mbedtls_ssl_get_ciphersuite( &ssl ) );
1048
1049     while( ( ret = mbedtls_ssl_write( &ssl, buf, len ) ) <= 0 )
1050     {
1051         if( ret == MBEDTLS_ERR_NET_CONN_RESET )
1052         {
1053             mbedtls_printf( " failed\n  ! peer closed the connection\n\n" );
1054             goto reset;
1055         }
1056
1057         if( ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1058         {
1059             mbedtls_printf( " failed\n  ! mbedtls_ssl_write returned %d\n\n", ret );
1060             goto exit;
1061         }
1062     }
1063
1064     len = ret;
1065     mbedtls_printf( " %d bytes written\n\n%s\n", len, (char *) buf );
1066
1067     mbedtls_printf( "  . Closing the connection..." );
1068
1069     while( ( ret = mbedtls_ssl_close_notify( &ssl ) ) < 0 )
1070     {
1071         if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
1072             ret != MBEDTLS_ERR_SSL_WANT_WRITE )
1073         {
1074             mbedtls_printf( " failed\n  ! mbedtls_ssl_close_notify returned %d\n\n", ret );
1075             goto exit;
1076         }
1077     }
1078
1079     mbedtls_printf( " ok\n" );
1080
1081     ret = 0;
1082     goto exit;
1083
1084 exit:
1085
1086 #ifdef MBEDTLS_ERROR_C
1087     if( ret != 0 )
1088     {
1089         char error_buf[100];
1090         mbedtls_strerror( ret, error_buf, 100 );
1091         mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
1092     }
1093 #endif
1094
1095     mbedtls_net_free( &client_fd );
1096     mbedtls_net_free( &listen_fd );
1097
1098     mbedtls_x509_crt_free( &srvcert );
1099     mbedtls_pk_free( &pkey );
1100     mbedtls_ssl_free( &ssl );
1101     mbedtls_ssl_config_free( &conf );
1102 #if defined(MBEDTLS_SSL_CACHE_C)
1103     mbedtls_ssl_cache_free( &cache );
1104 #endif
1105     mbedtls_ctr_drbg_free( &ctr_drbg );
1106     mbedtls_entropy_free( &entropy );
1107
1108 #if defined(_WIN32)
1109     mbedtls_printf( "  Press Enter to exit this program.\n" );
1110     fflush( stdout );
1111 #endif
1112
1113     return NULL;
1114 }
1115 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_CERTS_C && MBEDTLS_ENTROPY_C &&
1116           MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_SRV_C && MBEDTLS_NET_C &&
1117           MBEDTLS_RSA_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_X509_CRT_PARSE_C
1118           && MBEDTLS_FS_IO && MBEDTLS_PEM_PARSE_C */
1119
1120 /* **************************
1121  *
1122  *
1123  * CAinitSslAdapter test
1124  *
1125  *
1126  * *************************/
1127
1128 static int testCAinitSslAdapter()
1129 {
1130     int ret = 0;
1131
1132     CAEndpoint_t serverAddr;
1133     serverAddr.adapter = CA_ADAPTER_IP;
1134     serverAddr.flags = CA_SECURE;
1135     serverAddr.port = 4433;
1136     char addr[] = {0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x00}; // 127.0.0.1
1137     memcpy(serverAddr.addr, addr, sizeof(addr));
1138     serverAddr.ifindex = 0;
1139
1140     ret = CAinitSslAdapter();
1141     if (ret != 0 ||
1142         &g_caSslContext == NULL ||
1143         &g_caSslContext->crt == NULL ||
1144         &g_caSslContext->pkey == NULL ||
1145         &g_caSslContext->clientTlsConf == NULL ||
1146         &g_caSslContext->serverTlsConf == NULL ||
1147         &g_caSslContext->rnd == NULL ||
1148         &g_caSslContext->entropy == NULL)
1149     {
1150         ret = 1;
1151     }
1152
1153     // CAdeinitSslAdapter
1154     ca_mutex_lock(g_sslContextMutex);
1155     DeletePeerList();
1156     mbedtls_x509_crt_free(&g_caSslContext->crt);
1157     mbedtls_pk_free(&g_caSslContext->pkey);
1158     mbedtls_ssl_config_free(&g_caSslContext->clientTlsConf);
1159     mbedtls_ssl_config_free(&g_caSslContext->serverTlsConf);
1160     mbedtls_ctr_drbg_free(&g_caSslContext->rnd);
1161     mbedtls_entropy_free(&g_caSslContext->entropy);
1162     OICFree(g_caSslContext);
1163     g_caSslContext = NULL;
1164     ca_mutex_unlock(g_sslContextMutex);
1165     ca_mutex_free(g_sslContextMutex);
1166     g_sslContextMutex = NULL;
1167
1168     return ret;
1169 }
1170
1171 // CAinitTlsAdapter()
1172 TEST(TLSAdaper, Test_1)
1173 {
1174     int ret = 0xFF;
1175     ret = testCAinitSslAdapter();
1176     EXPECT_EQ(0, ret);
1177 }
1178
1179 /* **************************
1180  *
1181  *
1182  * CAsetSslAdapterCallbacks test
1183  *
1184  *
1185  * *************************/
1186
1187 static int testCAsetSslAdapterCallbacks()
1188 {
1189     int ret = 0xFF;
1190     CAEndpoint_t serverAddr;
1191     serverAddr.adapter = CA_ADAPTER_IP;
1192     serverAddr.flags = CA_SECURE;
1193     serverAddr.port = 4433;
1194     char addr[] = {0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x00}; // 127.0.0.1
1195     memcpy(serverAddr.addr, addr, sizeof(addr));
1196     serverAddr.ifindex = 0;
1197
1198     // CAinitSslAdapter
1199     g_sslContextMutex = ca_mutex_new();
1200     ca_mutex_lock(g_sslContextMutex);
1201     g_caSslContext = (SslContext_t *)OICCalloc(1, sizeof(SslContext_t));
1202     g_caSslContext->peerList = u_arraylist_create();
1203     mbedtls_entropy_init(&g_caSslContext->entropy);
1204     mbedtls_ctr_drbg_init(&g_caSslContext->rnd);
1205     unsigned char * seed = (unsigned char*) SEED;
1206     mbedtls_ctr_drbg_seed(&g_caSslContext->rnd, mbedtls_entropy_func_clutch,
1207                                   &g_caSslContext->entropy, seed, sizeof(SEED));
1208     mbedtls_ctr_drbg_set_prediction_resistance(&g_caSslContext->rnd, MBEDTLS_CTR_DRBG_PR_OFF);
1209     mbedtls_ssl_config_init(&g_caSslContext->clientTlsConf);
1210     mbedtls_ssl_config_defaults(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_IS_CLIENT,
1211                                     MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
1212     mbedtls_ssl_conf_psk_cb(&g_caSslContext->clientTlsConf, GetPskCredentialsCallback, NULL);
1213     mbedtls_ssl_conf_rng( &g_caSslContext->clientTlsConf, mbedtls_ctr_drbg_random,
1214                           &g_caSslContext->rnd);
1215     mbedtls_ssl_conf_curves(&g_caSslContext->clientTlsConf, curve[ADAPTER_CURVE_SECP256R1]);
1216     mbedtls_ssl_conf_min_version(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_MAJOR_VERSION_3,
1217                                  MBEDTLS_SSL_MINOR_VERSION_1);
1218     mbedtls_ssl_conf_renegotiation(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_RENEGOTIATION_DISABLED);
1219     mbedtls_ssl_conf_authmode(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_VERIFY_REQUIRED);
1220     CAsetTlsCipherSuite(MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256);
1221     mbedtls_x509_crt_init(&g_caSslContext->ca);
1222     mbedtls_x509_crt_init(&g_caSslContext->crt);
1223     mbedtls_pk_init(&g_caSslContext->pkey);
1224     mbedtls_x509_crl_init(&g_caSslContext->crl);
1225     ca_mutex_unlock(g_sslContextMutex);
1226
1227     CAsetSslAdapterCallbacks(CATCPPacketReceivedCB, CATCPPacketSendCB, (CATransportAdapter_t)0);
1228     if (g_caSslContext->adapterCallbacks[0].recvCallback == NULL &&
1229         g_caSslContext->adapterCallbacks[0].sendCallback == NULL &&
1230         g_caSslContext->adapterCallbacks[1].recvCallback == NULL &&
1231         g_caSslContext->adapterCallbacks[1].sendCallback == NULL)
1232     {
1233         ret = 0;
1234     }
1235     else
1236     {
1237         ret = 1;
1238     }
1239     CAsetSslAdapterCallbacks(CATCPPacketReceivedCB, CATCPPacketSendCB, CA_ADAPTER_IP);
1240     CAsetSslAdapterCallbacks(CATCPPacketReceivedCB, CATCPPacketSendCB, CA_ADAPTER_TCP);
1241     if (g_caSslContext->adapterCallbacks[0].recvCallback == CATCPPacketReceivedCB &&
1242         g_caSslContext->adapterCallbacks[0].sendCallback == CATCPPacketSendCB &&
1243         g_caSslContext->adapterCallbacks[1].recvCallback == CATCPPacketReceivedCB &&
1244         g_caSslContext->adapterCallbacks[1].sendCallback == CATCPPacketSendCB)
1245     {
1246         ret += 0;
1247     }
1248     else
1249     {
1250         ret += 1;
1251     }
1252
1253     // CAdeinitSslAdapter
1254     ca_mutex_lock(g_sslContextMutex);
1255     DeletePeerList();
1256     mbedtls_x509_crt_free(&g_caSslContext->crt);
1257     mbedtls_pk_free(&g_caSslContext->pkey);
1258     mbedtls_ssl_config_free(&g_caSslContext->clientTlsConf);
1259     mbedtls_ssl_config_free(&g_caSslContext->serverTlsConf);
1260     mbedtls_ctr_drbg_free(&g_caSslContext->rnd);
1261     mbedtls_entropy_free(&g_caSslContext->entropy);
1262     OICFree(g_caSslContext);
1263     g_caSslContext = NULL;
1264     ca_mutex_unlock(g_sslContextMutex);
1265     ca_mutex_free(g_sslContextMutex);
1266     g_sslContextMutex = NULL;
1267
1268     return ret;
1269 }
1270
1271 // CAsetSslAdapterCallbacks()
1272 TEST(TLSAdaper, Test_2)
1273 {
1274     int ret = 0xFF;
1275     ret = testCAsetSslAdapterCallbacks();
1276     EXPECT_EQ(0, ret);
1277 }
1278
1279 /* **************************
1280  *
1281  *
1282  * CAinitiateSslHandshake test
1283  *
1284  *
1285  * *************************/
1286
1287 unsigned char predictedClientHello[] = {
1288     0x16, 0x03, 0x01, 0x00, 0x63, 0x01, 0x00, 0x00, 0x5f, 0x03, 0x03, 0x57, 0xf2, 0x5f, 0x21, 0x04,
1289     0xb1, 0x3b, 0xda, 0x55, 0xa4, 0x8e, 0xcc, 0x3f, 0xe9, 0x45, 0x5c, 0xaf, 0xcb, 0x19, 0x2e, 0x1f,
1290     0x4b, 0xd5, 0x84, 0x5c, 0x4b, 0xd7, 0x7d, 0x38, 0xa2, 0xfa, 0x3d, 0x00, 0x00, 0x06, 0xc0, 0xae,
1291     0xc0, 0xae, 0x00, 0xff, 0x01, 0x00, 0x00, 0x30, 0x00, 0x0d, 0x00, 0x16, 0x00, 0x14, 0x06, 0x03,
1292     0x06, 0x01, 0x05, 0x03, 0x05, 0x01, 0x04, 0x03, 0x04, 0x01, 0x03, 0x03, 0x03, 0x01, 0x02, 0x03,
1293     0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00, 0x02, 0x00, 0x17, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00,
1294     0x00, 0x16, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00
1295 };
1296 static unsigned char controlBuf[sizeof(predictedClientHello)];
1297 static char controlBufLen = 0;
1298 static void CATCPPacketSendCB_forInitHsTest(CAEndpoint_t *, const void * buf, uint32_t buflen)
1299 {
1300     int n;
1301     n = write(sockfd, buf, buflen);
1302     if (n < 0)
1303          error("ERROR writing to socket");
1304
1305     memset(controlBuf, 0, sizeof(predictedClientHello));
1306     memcpy(controlBuf, buf, buflen);
1307     controlBufLen = buflen;
1308 }
1309
1310 static void * test0CAinitiateSslHandshake(void * arg)
1311 {
1312     CAEndpoint_t serverAddr;
1313     serverAddr.adapter = CA_ADAPTER_TCP;
1314     serverAddr.flags = CA_SECURE;
1315     serverAddr.port = 4433;
1316     char addr[] = {0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x00}; // 127.0.0.1
1317     memcpy(serverAddr.addr, addr, sizeof(addr));
1318     serverAddr.ifindex = 0;
1319
1320     // CAinitSslAdapter
1321     g_sslContextMutex = ca_mutex_new();
1322     ca_mutex_lock(g_sslContextMutex);
1323     g_caSslContext = (SslContext_t *)OICCalloc(1, sizeof(SslContext_t));
1324     g_caSslContext->peerList = u_arraylist_create();
1325     mbedtls_entropy_init(&g_caSslContext->entropy);
1326     mbedtls_ctr_drbg_init(&g_caSslContext->rnd);
1327     unsigned char * seed = (unsigned char*) SEED;
1328     mbedtls_ctr_drbg_seed(&g_caSslContext->rnd, mbedtls_entropy_func_clutch,
1329                                   &g_caSslContext->entropy, seed, sizeof(SEED));
1330     mbedtls_ctr_drbg_set_prediction_resistance(&g_caSslContext->rnd, MBEDTLS_CTR_DRBG_PR_OFF);
1331     mbedtls_ssl_config_init(&g_caSslContext->clientTlsConf);
1332     mbedtls_ssl_config_defaults(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_IS_CLIENT,
1333                                     MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
1334     mbedtls_ssl_conf_psk_cb(&g_caSslContext->clientTlsConf, GetPskCredentialsCallback, NULL);
1335     mbedtls_ssl_conf_rng( &g_caSslContext->clientTlsConf, mbedtls_ctr_drbg_random,
1336                           &g_caSslContext->rnd);
1337     mbedtls_ssl_conf_curves(&g_caSslContext->clientTlsConf, curve[ADAPTER_CURVE_SECP256R1]);
1338     mbedtls_ssl_conf_min_version(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_MAJOR_VERSION_3,
1339                                  MBEDTLS_SSL_MINOR_VERSION_1);
1340     mbedtls_ssl_conf_renegotiation(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_RENEGOTIATION_DISABLED);
1341     mbedtls_ssl_conf_authmode(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_VERIFY_REQUIRED);
1342     mbedtls_x509_crt_init(&g_caSslContext->ca);
1343     mbedtls_x509_crt_init(&g_caSslContext->crt);
1344     mbedtls_pk_init(&g_caSslContext->pkey);
1345     mbedtls_x509_crl_init(&g_caSslContext->crl);
1346     ca_mutex_unlock(g_sslContextMutex);
1347
1348     // CAsetSslAdapterCallbacks
1349     g_caSslContext->adapterCallbacks[1].recvCallback = CATCPPacketReceivedCB;
1350     g_caSslContext->adapterCallbacks[1].sendCallback = CATCPPacketSendCB_forInitHsTest;
1351
1352     // CAsetPkixInfoCallback
1353     g_getPkixInfoCallback = infoCallback_that_loads_x509;
1354
1355     // CAsetCredentialTypesCallback
1356     g_getCredentialTypesCallback = clutch;
1357
1358     // CAsetTlsCipherSuite
1359     mbedtls_ssl_conf_ciphersuites(&g_caSslContext->clientTlsConf,
1360                                          tlsCipher[ADAPTER_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8]);
1361     mbedtls_ssl_conf_ciphersuites(&g_caSslContext->serverTlsConf,
1362                                          tlsCipher[ADAPTER_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8]);
1363     g_caSslContext->cipher = ADAPTER_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8;
1364
1365     CAsetPskCredentialsCallback(GetDtlsPskCredentials);
1366
1367     socketConnect();
1368
1369     unsigned int unixTime = (unsigned)time(NULL);
1370     CAinitiateSslHandshake(&serverAddr);
1371     predictedClientHello[11] = unixTime >> 24;
1372     predictedClientHello[12] = (unixTime << 8) >> 24;
1373     predictedClientHello[13] = (unixTime << 16) >> 24;
1374     predictedClientHello[14] = (unixTime << 24) >> 24;
1375
1376     // CAcloseTlsConnection
1377     ca_mutex_lock(g_sslContextMutex);
1378     SslEndPoint_t * tep = GetSslPeer(&serverAddr);
1379     mbedtls_ssl_close_notify(&tep->ssl);
1380     RemovePeerFromList(&tep->sep.endpoint);
1381     ca_mutex_unlock(g_sslContextMutex);
1382
1383     // CAdeinitTlsAdapter
1384     ca_mutex_lock(g_sslContextMutex);
1385     DeletePeerList();
1386     mbedtls_x509_crt_free(&g_caSslContext->crt);
1387     mbedtls_pk_free(&g_caSslContext->pkey);
1388     mbedtls_ssl_config_free(&g_caSslContext->clientTlsConf);
1389     mbedtls_ssl_config_free(&g_caSslContext->serverTlsConf);
1390     mbedtls_ctr_drbg_free(&g_caSslContext->rnd);
1391     mbedtls_entropy_free(&g_caSslContext->entropy);
1392     OICFree(g_caSslContext);
1393     g_caSslContext = NULL;
1394     ca_mutex_unlock(g_sslContextMutex);
1395     ca_mutex_free(g_sslContextMutex);
1396     g_sslContextMutex = NULL;
1397
1398     socketClose();
1399
1400     if (controlBufLen == sizeof(predictedClientHello) &&
1401         memcmp(predictedClientHello, controlBuf, sizeof(predictedClientHello)) == 0)
1402     {
1403         *((int*)arg) = 0;
1404         return NULL;
1405     }
1406     else
1407     {
1408         *((int*)arg) = 0xFF;
1409         return (void *) 0xFF;
1410     }
1411 }
1412
1413 static int test1CAinitiateSslHandshake()
1414 {
1415     int ret = 0xff;
1416     ret = CAinitiateSslHandshake(NULL);
1417     if (CA_STATUS_INVALID_PARAM == ret)
1418     {
1419         ret = 0;
1420     }
1421     else
1422     {
1423         ret = 1;
1424     }
1425     return ret;
1426 }
1427
1428 // CAinitiateSslHandshake()
1429 TEST(TLSAdaper, Test_3_0)
1430 {
1431     pthread_t thread1, thread2;
1432     int ret = 0;
1433     int arg = 1;
1434
1435     ret = pthread_create( &thread1, NULL, server, (void*) NULL);
1436     if(ret)
1437     {
1438         fprintf(stderr, "Error - pthread_create() return code: %d\n", ret);
1439         exit(EXIT_FAILURE);
1440     }
1441
1442     sleep(5);
1443
1444     ret = pthread_create( &thread2, NULL, test0CAinitiateSslHandshake, &arg);
1445     if(ret)
1446     {
1447         fprintf(stderr, "Error - pthread_create() return code: %d\n", ret);
1448         exit(EXIT_FAILURE);
1449     }
1450
1451     sleep(5);
1452
1453     EXPECT_EQ(0, arg);
1454 }
1455
1456 TEST(TLSAdaper, Test_3_1)
1457 {
1458     int ret = 0xFF;
1459     ret = test1CAinitiateSslHandshake();
1460     EXPECT_EQ(0, ret);
1461 }
1462
1463 /* **************************
1464  *
1465  *
1466  * CAencryptSsl test
1467  *
1468  *
1469  * *************************/
1470
1471 static void * testCAencryptSsl(void * arg)
1472 {
1473     int ret = 0;
1474     CAEndpoint_t serverAddr;
1475     serverAddr.adapter = CA_ADAPTER_TCP;
1476     serverAddr.flags = CA_SECURE;
1477     serverAddr.port = 4433;
1478     char addr[] = {0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x00}; // 127.0.0.1
1479     memcpy(serverAddr.addr, addr, sizeof(addr));
1480     serverAddr.ifindex = 0;
1481
1482     // CAinitTlsAdapter
1483     g_sslContextMutex = ca_mutex_new();
1484     ca_mutex_lock(g_sslContextMutex);
1485     g_caSslContext = (SslContext_t *)OICCalloc(1, sizeof(SslContext_t));
1486     g_caSslContext->peerList = u_arraylist_create();
1487     mbedtls_entropy_init(&g_caSslContext->entropy);
1488     mbedtls_ctr_drbg_init(&g_caSslContext->rnd);
1489     unsigned char * seed = (unsigned char*) SEED;
1490     mbedtls_ctr_drbg_seed(&g_caSslContext->rnd, mbedtls_entropy_func_clutch,
1491                                   &g_caSslContext->entropy, seed, sizeof(SEED));
1492     mbedtls_ctr_drbg_set_prediction_resistance(&g_caSslContext->rnd, MBEDTLS_CTR_DRBG_PR_OFF);
1493     mbedtls_ssl_config_init(&g_caSslContext->clientTlsConf);
1494     mbedtls_ssl_config_defaults(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_IS_CLIENT,
1495                                     MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
1496     mbedtls_ssl_conf_psk_cb(&g_caSslContext->clientTlsConf, GetPskCredentialsCallback, NULL);
1497     mbedtls_ssl_conf_rng( &g_caSslContext->clientTlsConf, mbedtls_ctr_drbg_random,
1498                           &g_caSslContext->rnd);
1499     mbedtls_ssl_conf_curves(&g_caSslContext->clientTlsConf, curve[ADAPTER_CURVE_SECP256R1]);
1500     mbedtls_ssl_conf_min_version(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_MAJOR_VERSION_3,
1501                                  MBEDTLS_SSL_MINOR_VERSION_1);
1502     mbedtls_ssl_conf_renegotiation(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_RENEGOTIATION_DISABLED);
1503     mbedtls_ssl_conf_authmode(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_VERIFY_REQUIRED);
1504     CAsetTlsCipherSuite(MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256);
1505     mbedtls_x509_crt_init(&g_caSslContext->ca);
1506     mbedtls_x509_crt_init(&g_caSslContext->crt);
1507     mbedtls_pk_init(&g_caSslContext->pkey);
1508     mbedtls_x509_crl_init(&g_caSslContext->crl);
1509     ca_mutex_unlock(g_sslContextMutex);
1510
1511     CAsetSslAdapterCallbacks(CATCPPacketReceivedCB, CATCPPacketSendCB, CA_ADAPTER_TCP);
1512
1513     CAsetPkixInfoCallback(infoCallback_that_loads_x509);
1514
1515     // CAsetCredentialTypesCallback
1516     g_getCredentialTypesCallback = clutch;
1517
1518     CAsetTlsCipherSuite(MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8);
1519     CAsetPskCredentialsCallback(GetDtlsPskCredentials);
1520
1521     socketConnect();
1522
1523     // CAinitiateSslHandshake
1524     ca_mutex_lock(g_sslContextMutex);
1525     InitiateTlsHandshake(&serverAddr);
1526     ca_mutex_unlock(g_sslContextMutex);
1527
1528     unsigned char buffer[2048] = {'\0'};
1529     int buflen = 0;
1530     CASecureEndpoint_t * sep = (CASecureEndpoint_t *) malloc (sizeof(CASecureEndpoint_t));
1531     sep->endpoint = serverAddr;
1532
1533     for (int i = 0; i < 6; i++)
1534     {
1535         PacketReceive(buffer, &buflen);
1536         CAdecryptSsl(sep, (uint8_t *)buffer, buflen);
1537     }
1538
1539     ret = sprintf( (char*)buffer, GET_REQUEST );
1540
1541     CAencryptSsl(&serverAddr, buffer, ret);
1542
1543     PacketReceive(buffer, &buflen);
1544     CAdecryptSsl(sep, (uint8_t *)buffer, buflen);
1545
1546     CAcloseSslConnection(&serverAddr);
1547
1548     // CAdeinitSslAdapter
1549     ca_mutex_lock(g_sslContextMutex);
1550     DeletePeerList();
1551     mbedtls_x509_crt_free(&g_caSslContext->crt);
1552     mbedtls_pk_free(&g_caSslContext->pkey);
1553     mbedtls_ssl_config_free(&g_caSslContext->clientTlsConf);
1554     mbedtls_ssl_config_free(&g_caSslContext->serverTlsConf);
1555     mbedtls_ctr_drbg_free(&g_caSslContext->rnd);
1556     mbedtls_entropy_free(&g_caSslContext->entropy);
1557     OICFree(g_caSslContext);
1558     g_caSslContext = NULL;
1559     ca_mutex_unlock(g_sslContextMutex);
1560     ca_mutex_free(g_sslContextMutex);
1561     g_sslContextMutex = NULL;
1562
1563     socketClose();
1564
1565     if (control_server_message_len == msglen && memcmp(msg, control_server_message, control_server_message_len) == 0)
1566     {
1567         *((int*)arg) = 0;
1568         return NULL;
1569     }
1570     else
1571     {
1572         *((int*)arg) = 0xFF;
1573         return (void *) 0xFF;
1574     }
1575 }
1576
1577 // CAencryptSsl()
1578 TEST(TLSAdaper, Test_4)
1579 {
1580     pthread_t thread1, thread2;
1581     int ret = 0;
1582     int arg = 1;
1583
1584     ret = pthread_create( &thread1, NULL, server, (void*) NULL);
1585     if(ret)
1586     {
1587         fprintf(stderr,"Error - pthread_create() return code: %d\n", ret);
1588         exit(EXIT_FAILURE);
1589     }
1590
1591     sleep(5);
1592
1593     ret = pthread_create( &thread2, NULL, testCAencryptSsl, &arg);
1594     if(ret)
1595     {
1596         fprintf(stderr,"Error - pthread_create() return code: %d\n", ret);
1597         exit(EXIT_FAILURE);
1598     }
1599
1600     sleep(5);
1601
1602     EXPECT_EQ(0, arg);
1603 }
1604
1605 /* **************************
1606  *
1607  *
1608  * CAdecryptSsl test
1609  *
1610  *
1611  * *************************/
1612
1613 static void * testCAdecryptSsl(void * arg)
1614 {
1615     int ret = 0;
1616     unsigned char buffer[2048] = {'\0'};
1617     int buflen = 0;
1618
1619     CAEndpoint_t serverAddr;
1620     serverAddr.adapter = CA_ADAPTER_TCP;
1621     serverAddr.flags = CA_SECURE;
1622     serverAddr.port = 4433;
1623     char addr[] = {0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x00}; // 127.0.0.1
1624     memcpy(serverAddr.addr, addr, sizeof(addr));
1625     serverAddr.ifindex = 0;
1626
1627     // CAinitTlsAdapter
1628     g_sslContextMutex = ca_mutex_new();
1629     ca_mutex_lock(g_sslContextMutex);
1630     g_caSslContext = (SslContext_t *)OICCalloc(1, sizeof(SslContext_t));
1631     g_caSslContext->peerList = u_arraylist_create();
1632     mbedtls_entropy_init(&g_caSslContext->entropy);
1633     mbedtls_ctr_drbg_init(&g_caSslContext->rnd);
1634     unsigned char * seed = (unsigned char*) SEED;
1635     mbedtls_ctr_drbg_seed(&g_caSslContext->rnd, mbedtls_entropy_func_clutch,
1636                                   &g_caSslContext->entropy, seed, sizeof(SEED));
1637     mbedtls_ctr_drbg_set_prediction_resistance(&g_caSslContext->rnd, MBEDTLS_CTR_DRBG_PR_OFF);
1638     mbedtls_ssl_config_init(&g_caSslContext->clientTlsConf);
1639     mbedtls_ssl_config_defaults(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_IS_CLIENT,
1640                                     MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
1641     mbedtls_ssl_conf_psk_cb(&g_caSslContext->clientTlsConf, GetPskCredentialsCallback, NULL);
1642     mbedtls_ssl_conf_rng( &g_caSslContext->clientTlsConf, mbedtls_ctr_drbg_random,
1643                           &g_caSslContext->rnd);
1644     mbedtls_ssl_conf_curves(&g_caSslContext->clientTlsConf, curve[ADAPTER_CURVE_SECP256R1]);
1645     mbedtls_ssl_conf_min_version(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_MAJOR_VERSION_3,
1646                                  MBEDTLS_SSL_MINOR_VERSION_1);
1647     mbedtls_ssl_conf_renegotiation(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_RENEGOTIATION_DISABLED);
1648     mbedtls_ssl_conf_authmode(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_VERIFY_REQUIRED);
1649     CAsetTlsCipherSuite(MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256);
1650     mbedtls_x509_crt_init(&g_caSslContext->ca);
1651     mbedtls_x509_crt_init(&g_caSslContext->crt);
1652     mbedtls_pk_init(&g_caSslContext->pkey);
1653     mbedtls_x509_crl_init(&g_caSslContext->crl);
1654     ca_mutex_unlock(g_sslContextMutex);
1655
1656     // CAsetTlsAdapterCallbacks
1657     CAsetSslAdapterCallbacks(CATCPPacketReceivedCB, CATCPPacketSendCB, CA_ADAPTER_TCP);
1658
1659     // CAsetPkixInfoCallback
1660     CAsetPkixInfoCallback(infoCallback_that_loads_x509);
1661
1662     // CAsetCredentialTypesCallback
1663     g_getCredentialTypesCallback = clutch;
1664
1665     CAsetTlsCipherSuite(MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8);
1666
1667     CAsetPskCredentialsCallback(GetDtlsPskCredentials);
1668
1669     socketConnect();
1670
1671     // CAinitiateSslHandshake
1672     ca_mutex_lock(g_sslContextMutex);
1673     InitiateTlsHandshake(&serverAddr);
1674     ca_mutex_unlock(g_sslContextMutex);
1675
1676     CASecureEndpoint_t * sep = (CASecureEndpoint_t *) malloc (sizeof(CASecureEndpoint_t));
1677     sep->endpoint = serverAddr;
1678
1679     for (int i = 0; i < 6; i++)
1680     {
1681         PacketReceive(buffer, &buflen);
1682         CAdecryptSsl(sep, (uint8_t *)buffer, buflen);
1683     }
1684
1685     ret = sprintf((char*)buffer, GET_REQUEST);
1686
1687     CAencryptSsl(&serverAddr, buffer, ret);
1688
1689     PacketReceive(buffer, &buflen);
1690     CAdecryptSsl(sep, (uint8_t *)buffer, buflen);
1691
1692     CAcloseSslConnection(&serverAddr);
1693
1694     // CAdeinitSslAdapter
1695     ca_mutex_lock(g_sslContextMutex);
1696     DeletePeerList();
1697     mbedtls_x509_crt_free(&g_caSslContext->crt);
1698     mbedtls_pk_free(&g_caSslContext->pkey);
1699     mbedtls_ssl_config_free(&g_caSslContext->clientTlsConf);
1700     mbedtls_ssl_config_free(&g_caSslContext->serverTlsConf);
1701     mbedtls_ctr_drbg_free(&g_caSslContext->rnd);
1702     mbedtls_entropy_free(&g_caSslContext->entropy);
1703     OICFree(g_caSslContext);
1704     g_caSslContext = NULL;
1705     ca_mutex_unlock(g_sslContextMutex);
1706     ca_mutex_free(g_sslContextMutex);
1707     g_sslContextMutex = NULL;
1708
1709     socketClose();
1710
1711     if (control_server_message_len == msglen && memcmp(msg, control_server_message,
1712                                                                  control_server_message_len) == 0)
1713     {
1714         *((int*)arg) = 0;
1715         return NULL;
1716     }
1717     else
1718     {
1719         *((int*)arg) = 0xFF;
1720         return (void *) 0xFF;
1721     }
1722 }
1723
1724 // CAdecryptTls()
1725 TEST(TLSAdaper, Test_5)
1726 {
1727     pthread_t thread1, thread2;
1728     int ret = 0;
1729     int arg = 1;
1730
1731     ret = pthread_create( &thread1, NULL, server, (void*) NULL);
1732     if(ret)
1733     {
1734         fprintf(stderr,"Error - pthread_create() return code: %d\n", ret);
1735         exit(EXIT_FAILURE);
1736     }
1737
1738     sleep(5);
1739
1740     ret = pthread_create( &thread2, NULL, testCAdecryptSsl, &arg);
1741     if(ret)
1742     {
1743         fprintf(stderr,"Error - pthread_create() return code: %d\n", ret);
1744         exit(EXIT_FAILURE);
1745     }
1746
1747     sleep(5);
1748
1749     EXPECT_EQ(0, arg);
1750 }
1751
1752 /* **************************
1753  *
1754  *
1755  * CAdeinitSslAdapter test
1756  *
1757  *
1758  * *************************/
1759
1760 static int testCAdeinitSslAdapter()
1761 {
1762     int ret = 0;
1763     CAEndpoint_t serverAddr;
1764     serverAddr.adapter = CA_ADAPTER_IP;
1765     serverAddr.flags = CA_SECURE;
1766     serverAddr.port = 4433;
1767     char addr[] = {0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x00}; // 127.0.0.1
1768     memcpy(serverAddr.addr, addr, sizeof(addr));
1769     serverAddr.ifindex = 0;
1770
1771     // CAinitTlsAdapter
1772     g_sslContextMutex = ca_mutex_new();
1773     ca_mutex_lock(g_sslContextMutex);
1774     g_caSslContext = (SslContext_t *)OICCalloc(1, sizeof(SslContext_t));
1775     g_caSslContext->peerList = u_arraylist_create();
1776     mbedtls_entropy_init(&g_caSslContext->entropy);
1777     mbedtls_ctr_drbg_init(&g_caSslContext->rnd);
1778     unsigned char * seed = (unsigned char*) SEED;
1779     mbedtls_ctr_drbg_seed(&g_caSslContext->rnd, mbedtls_entropy_func,
1780                                   &g_caSslContext->entropy, seed, sizeof(SEED));
1781     mbedtls_ctr_drbg_set_prediction_resistance(&g_caSslContext->rnd, MBEDTLS_CTR_DRBG_PR_OFF);
1782     mbedtls_ssl_config_init(&g_caSslContext->clientTlsConf);
1783     mbedtls_ssl_config_defaults(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_IS_CLIENT,
1784                                     MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
1785     mbedtls_ssl_conf_psk_cb(&g_caSslContext->clientTlsConf, GetPskCredentialsCallback, NULL);
1786     mbedtls_ssl_conf_rng( &g_caSslContext->clientTlsConf, mbedtls_ctr_drbg_random,
1787                           &g_caSslContext->rnd);
1788     mbedtls_ssl_conf_curves(&g_caSslContext->clientTlsConf, curve[ADAPTER_CURVE_SECP256R1]);
1789     mbedtls_ssl_conf_min_version(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_MAJOR_VERSION_3,
1790                                  MBEDTLS_SSL_MINOR_VERSION_1);
1791     mbedtls_ssl_conf_renegotiation(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_RENEGOTIATION_DISABLED);
1792     mbedtls_ssl_conf_authmode(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_VERIFY_REQUIRED);
1793     mbedtls_x509_crt_init(&g_caSslContext->ca);
1794     mbedtls_x509_crt_init(&g_caSslContext->crt);
1795     mbedtls_pk_init(&g_caSslContext->pkey);
1796     mbedtls_x509_crl_init(&g_caSslContext->crl);
1797     ca_mutex_unlock(g_sslContextMutex);
1798
1799     // CAsetTlsAdapterCallbacks
1800     g_caSslContext->adapterCallbacks[1].recvCallback = CATCPPacketReceivedCB;
1801     g_caSslContext->adapterCallbacks[1].sendCallback = CATCPPacketSendCB;
1802
1803     // CAsetPkixInfoCallback
1804     g_getPkixInfoCallback = infoCallback_that_loads_x509;
1805
1806     // CAsetTlsCipherSuite
1807     mbedtls_ssl_conf_ciphersuites(&g_caSslContext->clientTlsConf,
1808                                          tlsCipher[ADAPTER_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8]);
1809     mbedtls_ssl_conf_ciphersuites(&g_caSslContext->serverTlsConf,
1810                                          tlsCipher[ADAPTER_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8]);
1811     g_caSslContext->cipher = ADAPTER_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8;
1812
1813     CAdeinitSslAdapter();
1814
1815     if (g_caSslContext != NULL ||
1816         g_sslContextMutex != NULL)
1817     {
1818         ret = 1;
1819     }
1820     else
1821     {
1822         ret = 0;
1823     }
1824     return ret;
1825 }
1826
1827 // CAdeinitSslAdapter()
1828 TEST(TLSAdaper, Test_6)
1829 {
1830     int ret = 0xFF;
1831     ret = testCAdeinitSslAdapter();
1832     EXPECT_EQ(0, ret);
1833 }
1834
1835 /* **************************
1836  *
1837  *
1838  * Server side test
1839  *
1840  *
1841  * *************************/
1842
1843 static void * testServer(void * arg)
1844 {
1845     CAEndpoint_t serverAddr;
1846     serverAddr.adapter = CA_ADAPTER_TCP;
1847     serverAddr.flags = CA_SECURE;
1848     serverAddr.port = 4432;
1849     char addr[] = {0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x00}; // 127.0.0.1
1850     memcpy(serverAddr.addr, addr, sizeof(addr));
1851     serverAddr.ifindex = 0;
1852     unsigned char buffer[2048] = {'\0'};
1853     int buflen = 0;
1854
1855     CAinitSslAdapter();
1856
1857     CAsetSslAdapterCallbacks(CATCPPacketReceivedCB_server, CATCPPacketSendCB_server, CA_ADAPTER_TCP);
1858     CAsetPkixInfoCallback(infoCallback_that_loads_x509);
1859
1860     // CAsetCredentialTypesCallback
1861     g_getCredentialTypesCallback = clutch;
1862
1863     CAsetTlsCipherSuite(MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8);
1864
1865     CAsetPskCredentialsCallback(GetDtlsPskCredentials);
1866
1867     socketOpen_server();
1868
1869     CASecureEndpoint_t * sep = (CASecureEndpoint_t *) malloc (sizeof(CASecureEndpoint_t));
1870     sep->endpoint = serverAddr;
1871
1872     for (int i = 0; i < 7; i++)
1873     {
1874         PacketReceive_server(buffer, &buflen);
1875         CAdecryptSsl(sep, (uint8_t *)buffer, buflen);
1876     }
1877
1878     CAcloseSslConnection(&serverAddr);
1879
1880     // CAdeinitSslAdapter
1881     ca_mutex_lock(g_sslContextMutex);
1882     DeletePeerList();
1883     mbedtls_x509_crt_free(&g_caSslContext->crt);
1884     mbedtls_pk_free(&g_caSslContext->pkey);
1885     mbedtls_ssl_config_free(&g_caSslContext->clientTlsConf);
1886     mbedtls_ssl_config_free(&g_caSslContext->serverTlsConf);
1887     mbedtls_ctr_drbg_free(&g_caSslContext->rnd);
1888     mbedtls_entropy_free(&g_caSslContext->entropy);
1889     OICFree(g_caSslContext);
1890     g_caSslContext = NULL;
1891     ca_mutex_unlock(g_sslContextMutex);
1892     ca_mutex_free(g_sslContextMutex);
1893     g_sslContextMutex = NULL;
1894
1895     socketClose_server();
1896
1897     if (control_client_message_len == msglen && memcmp(msg, control_client_message,
1898                                                             control_client_message_len) == 0)
1899     {
1900         *((int*)arg) = 0;
1901         return NULL;
1902     }
1903     else
1904     {
1905         *((int*)arg) = 0xFF;
1906         return (void *) 0xFF;
1907     }
1908 }
1909
1910 TEST(TLSAdaper, Test_7)
1911 {
1912     pthread_t thread1, thread2;
1913     int ret = 0;
1914     int arg = 1;
1915
1916     ret = pthread_create( &thread1, NULL, testServer, &arg);
1917     if (ret)
1918     {
1919         fprintf(stderr,"Error - pthread_create() return code: %d\n", ret);
1920         exit(EXIT_FAILURE);
1921     }
1922
1923     sleep(5);
1924
1925     ret = pthread_create( &thread2, NULL, client, (void*) NULL);
1926     if (ret)
1927     {
1928         fprintf(stderr,"Error - pthread_create() return code: %d\n", ret);
1929         exit(EXIT_FAILURE);
1930     }
1931
1932     sleep(5);
1933
1934     EXPECT_EQ(NULL, arg);
1935 }
1936
1937 /* **************************
1938  *
1939  * CAsetPskCredentialsCallback test
1940  * CAsetPkixInfoCallback test
1941  * CAsetCredentialTypesCallback test
1942  *
1943  * *************************/
1944
1945 static int testCAsetPskCredentialsCallback()
1946 {
1947     static CAgetPskCredentialsHandler credCallback = (CAgetPskCredentialsHandler)dummyHandler;
1948     CAsetPskCredentialsCallback(credCallback);
1949     if (g_getCredentialsCallback == (CAgetPskCredentialsHandler)dummyHandler)
1950     {
1951         return 0;
1952     }
1953     else
1954     {
1955         return 1;
1956     }
1957 }
1958
1959 static int testCAsetPkixInfoCallback()
1960 {
1961     static CAgetPkixInfoHandler infoCallback = (CAgetPkixInfoHandler)dummyHandler;
1962     CAsetPkixInfoCallback(infoCallback);
1963     if (g_getPkixInfoCallback == (CAgetPkixInfoHandler)dummyHandler)
1964     {
1965         return 0;
1966     }
1967     else
1968     {
1969         return 1;
1970     }
1971 }
1972
1973 static int testCAsetCredentialTypesCallback()
1974 {
1975     static CAgetCredentialTypesHandler credTypesCallback = (CAgetCredentialTypesHandler)dummyHandler;
1976     CAsetCredentialTypesCallback(credTypesCallback);
1977     if (g_getCredentialTypesCallback == (CAgetCredentialTypesHandler)dummyHandler)
1978     {
1979         return 0;
1980     }
1981     else
1982     {
1983         return 1;
1984     }
1985 }
1986
1987 // CAsetPskCredentialsCallback()
1988 TEST(TLSAdaper, Test_9_0)
1989 {
1990     int ret = 0xFF;
1991     ret = testCAsetPskCredentialsCallback();
1992     EXPECT_EQ(0, ret);
1993 }
1994 // CAsetPkixInfoCallback()
1995 TEST(TLSAdaper, Test_9_1)
1996 {
1997     int ret = 0xFF;
1998     ret = testCAsetPkixInfoCallback();
1999     EXPECT_EQ(0, ret);
2000 }
2001 // CAsetCredentialTypesCallback()
2002 TEST(TLSAdaper, Test_9_2)
2003 {
2004     int ret = 0xFF;
2005     ret = testCAsetCredentialTypesCallback();
2006     EXPECT_EQ(0, ret);
2007 }
2008
2009 /* **************************
2010  *
2011  *
2012  * CAsetTlsCipherSuite test
2013  *
2014  *
2015  * *************************/
2016
2017 static int testCAsetTlsCipherSuite()
2018 {
2019     int ret = 0, status = 0;
2020     CAEndpoint_t serverAddr;
2021     serverAddr.adapter = CA_ADAPTER_TCP;
2022     serverAddr.flags = CA_SECURE;
2023     serverAddr.port = 4433;
2024     char addr[] = {0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x00}; // 127.0.0.1
2025     memcpy(serverAddr.addr, addr, sizeof(addr));
2026     serverAddr.ifindex = 0;
2027
2028     CAinitSslAdapter();
2029
2030     // CAsetCredentialTypesCallback
2031     g_getCredentialTypesCallback = clutch;
2032
2033     status = CAsetTlsCipherSuite(MBEDTLS_TLS_RSA_WITH_AES_256_CBC_SHA);
2034     if (ADAPTER_TLS_RSA_WITH_AES_256_CBC_SHA != g_caSslContext->cipher || status != CA_STATUS_OK)
2035     {
2036         ret += 1;
2037     }
2038
2039     status = CAsetTlsCipherSuite(MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8);
2040     if (ADAPTER_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 != g_caSslContext->cipher || status != CA_STATUS_OK)
2041     {
2042         ret += 1;
2043     }
2044
2045     status = CAsetTlsCipherSuite(MBEDTLS_TLS_ECDH_ANON_WITH_AES_128_CBC_SHA256);
2046     if (ADAPTER_TLS_ECDH_ANON_WITH_AES_128_CBC_SHA_256 != g_caSslContext->cipher || status != CA_STATUS_OK)
2047     {
2048         ret += 1;
2049     }
2050
2051     status = CAsetTlsCipherSuite(MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256);
2052     if (ADAPTER_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 != g_caSslContext->cipher || status != CA_STATUS_OK)
2053     {
2054         ret += 1;
2055     }
2056
2057     status = CAsetTlsCipherSuite(dummyHandler);
2058     if (CA_STATUS_FAILED != status)
2059     {
2060         ret += 1;
2061     }
2062
2063     // CAdeinitSslAdapter
2064     ca_mutex_lock(g_sslContextMutex);
2065     DeletePeerList();
2066     mbedtls_x509_crt_free(&g_caSslContext->crt);
2067     mbedtls_pk_free(&g_caSslContext->pkey);
2068     mbedtls_ssl_config_free(&g_caSslContext->clientTlsConf);
2069     mbedtls_ssl_config_free(&g_caSslContext->serverTlsConf);
2070     mbedtls_ctr_drbg_free(&g_caSslContext->rnd);
2071     mbedtls_entropy_free(&g_caSslContext->entropy);
2072     OICFree(g_caSslContext);
2073     g_caSslContext = NULL;
2074     ca_mutex_unlock(g_sslContextMutex);
2075     ca_mutex_free(g_sslContextMutex);
2076     g_sslContextMutex = NULL;
2077
2078     return ret;
2079 }
2080
2081 // CAinitTlsAdapter()
2082 TEST(TLSAdaper, Test_10)
2083 {
2084     int ret = 0xff;
2085     ret = testCAsetTlsCipherSuite();
2086     EXPECT_EQ(0, ret);
2087 }
2088
2089 static void * testCAsslGenerateOwnerPsk(void * arg)
2090 {
2091     int ret = 0;
2092     CAEndpoint_t serverAddr;
2093     serverAddr.adapter = CA_ADAPTER_TCP;
2094     serverAddr.flags = CA_SECURE;
2095     serverAddr.port = 4433;
2096     char addr[] = {0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x00}; // 127.0.0.1
2097     memcpy(serverAddr.addr, addr, sizeof(addr));
2098     serverAddr.ifindex = 0;
2099
2100     uint8_t label[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A};
2101     uint8_t rsrcServerDeviceId[] = {0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A};
2102     uint8_t provServerDeviceId[] = {0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A};
2103     uint8_t ownerPsk[0x100] = {0x00};
2104     uint8_t predictedPSK[] = {
2105         0xba, 0x72, 0x16, 0xbc, 0x7f, 0x8c, 0xfe, 0xfc, 0xd0, 0xac, 0x1a, 0x37, 0xad, 0x60, 0xe8, 0x9e,
2106         0xb3, 0x31, 0xa2, 0x30, 0xaf, 0x68, 0xc9, 0xa6, 0x89, 0x8a, 0x04, 0x21, 0x6c, 0xbd, 0x04, 0x08,
2107         0x68, 0x11, 0x54, 0x9e, 0x2a, 0x10, 0x91, 0x94, 0x3c, 0x44, 0x52, 0xc7, 0xfa, 0x78, 0x44, 0x87,
2108         0xea, 0x30, 0x08, 0x5f, 0xc1, 0x64, 0xaa, 0x0d, 0xfd, 0x84, 0x16, 0x83, 0x20, 0xc9, 0x08, 0x65,
2109         0xd2, 0x4a, 0x55, 0x9e, 0x8f, 0x88, 0x3c, 0x57, 0x10, 0xbd, 0x5a, 0x30, 0x01, 0xb4, 0x59, 0x63,
2110         0x64, 0x19, 0x8d, 0xfa, 0x5c, 0x86, 0x92, 0xf7, 0x60, 0x99, 0xdb, 0xae, 0x0e, 0xad, 0x80, 0xf1,
2111         0x82, 0xaf, 0x1b, 0x14, 0x0c, 0x99, 0x13, 0x53, 0x54, 0x33, 0x6a, 0x17, 0x24, 0x5c, 0x9d, 0xdb,
2112         0x5a, 0xfb, 0x73, 0x2f, 0x41, 0xe8, 0xeb, 0x2e, 0x68, 0xfe, 0xee, 0x0b, 0xdc, 0x54, 0x50, 0xf1,
2113         0x1e, 0x16, 0x19, 0x2c, 0x4e, 0xb6, 0x97, 0x9f, 0x9c, 0x32, 0x9c, 0x0e, 0xe0, 0xe1, 0x32, 0x64,
2114         0x16, 0x34, 0x53, 0x8e, 0xc5, 0xe3, 0xe5, 0xbc, 0x2c, 0x10, 0xae, 0x81, 0x2c, 0x1a, 0xb2, 0xb7,
2115         0xa3, 0xbe, 0x0f, 0xab, 0xfd, 0xf7, 0x87, 0x53, 0xcd, 0x3e, 0x31, 0xfb, 0x2d, 0x69, 0x6a, 0xd5,
2116         0xc3, 0x27, 0x04, 0x2b, 0x37, 0x02, 0x91, 0x05, 0x0c, 0x4e, 0x2a, 0xfc, 0x6c, 0x42, 0xe8, 0x37,
2117         0x23, 0x2f, 0x60, 0x6e, 0x0c, 0xed, 0x7c, 0xe0, 0x5f, 0x47, 0xb3, 0x51, 0x86, 0x5b, 0x26, 0x08,
2118         0x2a, 0x05, 0x89, 0xb0, 0xdd, 0x6f, 0xc6, 0x76, 0xc5, 0x2a, 0x60, 0x07, 0x0e, 0xb1, 0x71, 0x67,
2119         0x21, 0x11, 0xf8, 0xb5, 0x52, 0xa3, 0xf3, 0xf0, 0xd4, 0x5f, 0xdf, 0x44, 0x66, 0x23, 0xd8, 0x4e,
2120         0xbd, 0x64, 0x39, 0x43, 0x03, 0x37, 0xaa, 0xd7, 0xea, 0xb3, 0x6d, 0x2f, 0x84, 0x9c, 0x02, 0x49
2121     };
2122
2123     // CAinitTlsAdapter
2124     g_sslContextMutex = ca_mutex_new();
2125     ca_mutex_lock(g_sslContextMutex);
2126     g_caSslContext = (SslContext_t *)OICCalloc(1, sizeof(SslContext_t));
2127     g_caSslContext->peerList = u_arraylist_create();
2128     mbedtls_entropy_init(&g_caSslContext->entropy);
2129     mbedtls_ctr_drbg_init(&g_caSslContext->rnd);
2130     unsigned char * seed = (unsigned char*) SEED;
2131     mbedtls_ctr_drbg_seed(&g_caSslContext->rnd, mbedtls_entropy_func_clutch,
2132                                   &g_caSslContext->entropy, seed, sizeof(SEED));
2133     mbedtls_ctr_drbg_set_prediction_resistance(&g_caSslContext->rnd, MBEDTLS_CTR_DRBG_PR_OFF);
2134     mbedtls_ssl_config_init(&g_caSslContext->clientTlsConf);
2135     mbedtls_ssl_config_defaults(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_IS_CLIENT,
2136                                     MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT);
2137     mbedtls_ssl_conf_psk_cb(&g_caSslContext->clientTlsConf, GetPskCredentialsCallback, NULL);
2138     mbedtls_ssl_conf_rng( &g_caSslContext->clientTlsConf, mbedtls_ctr_drbg_random,
2139                           &g_caSslContext->rnd);
2140     mbedtls_ssl_conf_curves(&g_caSslContext->clientTlsConf, curve[ADAPTER_CURVE_SECP256R1]);
2141     mbedtls_ssl_conf_min_version(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_MAJOR_VERSION_3,
2142                                  MBEDTLS_SSL_MINOR_VERSION_1);
2143     mbedtls_ssl_conf_renegotiation(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_RENEGOTIATION_DISABLED);
2144     mbedtls_ssl_conf_authmode(&g_caSslContext->clientTlsConf, MBEDTLS_SSL_VERIFY_REQUIRED);
2145     CAsetTlsCipherSuite(MBEDTLS_TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256);
2146     mbedtls_x509_crt_init(&g_caSslContext->ca);
2147     mbedtls_x509_crt_init(&g_caSslContext->crt);
2148     mbedtls_pk_init(&g_caSslContext->pkey);
2149     mbedtls_x509_crl_init(&g_caSslContext->crl);
2150     ca_mutex_unlock(g_sslContextMutex);
2151
2152     // CAsetTlsAdapterCallbacks
2153     CAsetSslAdapterCallbacks(CATCPPacketReceivedCB, CATCPPacketSendCB, CA_ADAPTER_TCP);
2154
2155     // CAsetPkixInfoCallback
2156     CAsetPkixInfoCallback(infoCallback_that_loads_x509);
2157
2158     // CAsetCredentialTypesCallback
2159     g_getCredentialTypesCallback = clutch;
2160
2161     CAsetTlsCipherSuite(MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8);
2162
2163     CAsetPskCredentialsCallback(GetDtlsPskCredentials);
2164
2165     socketConnect();
2166
2167     // CAinitiateSslHandshake
2168     ca_mutex_lock(g_sslContextMutex);
2169     InitiateTlsHandshake(&serverAddr);
2170     ca_mutex_unlock(g_sslContextMutex);
2171
2172     ret = CAsslGenerateOwnerPsk(&serverAddr,
2173           label, sizeof(label),
2174           rsrcServerDeviceId, sizeof(rsrcServerDeviceId),
2175           provServerDeviceId, sizeof(provServerDeviceId),
2176           ownerPsk, 0x100);
2177
2178     // CAcloseTlsConnection
2179     ca_mutex_lock(g_sslContextMutex);
2180     SslEndPoint_t * tep = GetSslPeer(&serverAddr);
2181     mbedtls_ssl_close_notify(&tep->ssl);
2182     RemovePeerFromList(&tep->sep.endpoint);
2183     ca_mutex_unlock(g_sslContextMutex);
2184
2185     // CAdeinitTlsAdapter
2186     ca_mutex_lock(g_sslContextMutex);
2187     DeletePeerList();
2188     mbedtls_x509_crt_free(&g_caSslContext->crt);
2189     mbedtls_pk_free(&g_caSslContext->pkey);
2190     mbedtls_ssl_config_free(&g_caSslContext->clientTlsConf);
2191     mbedtls_ssl_config_free(&g_caSslContext->serverTlsConf);
2192     mbedtls_ctr_drbg_free(&g_caSslContext->rnd);
2193     mbedtls_entropy_free(&g_caSslContext->entropy);
2194     OICFree(g_caSslContext);
2195     g_caSslContext = NULL;
2196     ca_mutex_unlock(g_sslContextMutex);
2197     ca_mutex_free(g_sslContextMutex);
2198     g_sslContextMutex = NULL;
2199
2200     socketClose();
2201
2202     if (ret == 0 && memcmp(predictedPSK, ownerPsk, sizeof(predictedPSK)) == 0)
2203     {
2204         *((int*)arg) = 0;
2205         return NULL;
2206     }
2207     else
2208     {
2209         *((int*)arg) = 0xFF;
2210         return (void *) 0xFF;
2211     }
2212 }
2213
2214 TEST(TLSAdaper, Test_11)
2215 {
2216     pthread_t thread1, thread2;
2217     int ret = 0;
2218     int arg = 1;
2219
2220     ret = pthread_create( &thread1, NULL, server, (void*) NULL);
2221     if(ret)
2222     {
2223         fprintf(stderr, "Error - pthread_create() return code: %d\n", ret);
2224         exit(EXIT_FAILURE);
2225     }
2226
2227     sleep(5);
2228
2229     ret = pthread_create( &thread2, NULL, testCAsslGenerateOwnerPsk, &arg);
2230     if(ret)
2231     {
2232         fprintf(stderr, "Error - pthread_create() return code: %d\n", ret);
2233         exit(EXIT_FAILURE);
2234     }
2235
2236     sleep(5);
2237
2238     EXPECT_EQ(0, arg);
2239 }