-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lab_solution_06.cpp
174 lines (156 loc) · 3.6 KB
/
Lab_solution_06.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
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
//--- 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";
// throw -1;
//throw std::exception("Unknown Method");
//throw std::logic_error("Unknown Method");
//throw std::runtime_error("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 SSLClient
{
public:
SSLClient(SSL_METHOD method)
{
printf("\t[1] Create new SSL context\n");
ctx = SSL_CTX_new(method);
}
void connect()
{
printf("\t[2] Connect\n");
SSL_connect(ctx);
}
void write(const void *buf, int len)
{
printf("\t[3] Write\n");
SSL_unsafe_write(ctx, buf, len);
}
~SSLClient()
{
printf("\t[4] Clean up resources\n");
SSL_free(ctx);
}
private:
SSL_CTX* ctx = nullptr;
};
int main()
{
try
{
SSLClient client = SSLClient(METHOD_UNKNOWN);
client.connect();
client.write("Hello", 5);
}
catch(const std::exception& e)
{
std::cout << "Exception caught!" << "\n";
}
catch(char const* err)
{
std::cout << "Exception caught: "<< err << "\n";
}
std::cout << "After try/catch block" << "\n";
}