-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lab_03.cpp
119 lines (104 loc) · 2.64 KB
/
Lab_03.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
#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");
return NULL;
}
ctx = (SSL_CTX*) malloc(sizeof(SSL_CTX));
if(ctx == NULL) {
printf("Failed: Not enough memory\n");
return NULL;
}
ctx->peer = (X509*) malloc(sizeof(X509));
if(ctx->peer == NULL) {
printf("Failed: Not enough memory\n");
free(ctx);
return NULL;
}
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");
return -1;
}
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;
}
const int 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");
return -1;
}
printf("Data sent\n");
return 0;
}
//---
int main()
{
printf("Client Started...\n");
printf("\t[1] Create new SSL context\n");
SSL_CTX* ctx = SSL_CTX_new(METHOD_UNKNOWN);
printf("\t[2] Connect\n");
SSL_connect(ctx);
printf("\t[3] Write\n");
SSL_unsafe_write(ctx,"Hello",5);
printf("\t[4] Clean up resources\n");
SSL_free(ctx);
printf("Client Exit.\n");
}