-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lab_07.cpp
217 lines (191 loc) · 4.36 KB
/
Lab_07.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <iostream>
#include <string>
#include <time.h>
#include <errno.h>
using namespace std;
//--- SSL Library Stub ---
#define SSL_MAX_SSL_SESSION_ID_LENGTH 32
typedef enum
{
METHOD_UNKNOWN = 0,
METHOD_SSL_V3,
METHOD_DTLS_V2
} SSL_METHOD;
typedef struct
{
int length;
unsigned char* public_key;
} X509;
typedef struct
{
int ssl_version;
SSL_METHOD method;
unsigned char session_id[SSL_MAX_SSL_SESSION_ID_LENGTH];
long timeout;
X509* peer;
} SSL_CTX;
void generate_session_id(unsigned char* sid)
{
srand(time(0));
for(int i=0; i<SSL_MAX_SSL_SESSION_ID_LENGTH; i++)
{
sid[i] = 'a' + rand()%26;
}
}
void print_session_id(unsigned char* sid)
{
printf("Session id: ");
for(int i=0; i<SSL_MAX_SSL_SESSION_ID_LENGTH; i++)
{
printf("%c", sid[i]);
}
printf("\n");
}
SSL_CTX* SSL_CTX_new(SSL_METHOD method)
{
SSL_CTX *ctx = NULL;
if(method == METHOD_UNKNOWN)
{
printf("Failed: Unknown Method\n");
throw "Unknown Method";
}
ctx = (SSL_CTX*) malloc(sizeof(SSL_CTX));
if(ctx == NULL)
{
printf("Failed: Not enough memory\n");
throw "Not enough memory";
}
ctx->peer = (X509*) malloc(sizeof(X509));
if(ctx->peer == NULL)
{
printf("Failed: Not enough memory\n");
free(ctx);
throw "Not enough memory";
}
ctx->ssl_version = 3;
ctx->method = method;
generate_session_id(ctx->session_id);
ctx->timeout = 100;
return ctx;
}
void SSL_free(SSL_CTX* ctx)
{
if(ctx == NULL)
{
return;
}
free(ctx->peer);
free(ctx);
}
const int SSL_connect(SSL_CTX* ctx)
{
if(ctx == NULL)
{
printf("SSL context is NULL\n");
throw "Context is null";
}
print_session_id(ctx->session_id);
printf("Connected\n");
return 0;
}
const int record_send(unsigned char* session_id, const void *buf, int len)
{
print_session_id(session_id);
printf("Send <%s>\n", buf);
return 0;
}
void SSL_unsafe_write(SSL_CTX* ctx, const void *buf, int len)
{
int ret;
ret = record_send(ctx->session_id, buf, len);
if(ret < 0)
{
printf("Failed: Cannot send data\n");
throw "Data not sent";
}
printf("Data sent\n");
}
//---
class Device
{
public:
std::string getModel()
{
return m_model;
}
int getSystemID()
{
return m_systemID;
}
void setModel(std::string const model) { m_model = model; }
void setSystemID(int systemID) { m_systemID = systemID; }
protected:
std::string m_model;
private:
int m_systemID = 0;
};
class Client
{
public:
Client() {}
Client(SSL_METHOD method)
{
cout << "Client Constructor" << "\n";
ctx = SSL_CTX_new(method);
m_init = true;
}
void connect()
{
if(m_init)
{
cout << "Client connect" << "\n";
SSL_connect(ctx);
m_isConnected = true;
}
else
{
throw runtime_error("Client not initialized");
}
}
void write(const void *buf, int len)
{
cout << "Client write" << "\n";
SSL_unsafe_write(ctx, buf, len);
}
void shutdown()
{
cout << "Shutdown Client" << "\n";
m_isConnected = false;
}
bool getConnStatus()
{
return m_isConnected;
}
void setConnStatus(bool isConnected) { m_isConnected = isConnected; }
~Client()
{
cout << "Client Destructor" << "\n";
SSL_free(ctx);
}
private:
bool m_init = false;
bool m_isConnected = false;
SSL_CTX* ctx = nullptr;
};
class Lightbulb : public Device, public Client
{
public:
Lightbulb(){};
Lightbulb(SSL_METHOD method):Client(method){};
bool getBrightness()
{
return m_brightness;
}
private:
int m_brightness;
};
int main()
{
Lightbulb myLightbulb;
myLightbulb.connect();
}