summaryrefslogtreecommitdiffstats
path: root/pica-registrar/PICA_registrar.cpp
blob: 608708e6d283fb99fee1bb3f49e1f5ffb279538a (plain) (blame)
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


#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/select.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <sys/errno.h>

#include <cstring>
#include <cstdlib>
#include <map>

using namespace std;

#define PICA_REGISTRAR_PORT 2299
#define READ_BUF_SIZE 4096
#define WRITE_BUF_SIZE 8192

char read_buf[READ_BUF_SIZE];

char write_buf[WRITE_BUF_SIZE];


unsigned int current_id = 30 000;

int read_CSR(int client_socket, int *bytes_read)
{
int pos = 0;
int disconnect_flag = 0;
		 do 
			{
			 ret = recv(client_socket, read_buf + pos, READ_BUF_SIZE - 1 - pos, 0);
			 
			 if (ret > 0)
				pos += ret;
			 else
			 	{
				disconnect_flag = 1;
			 	if (ret < 0)
					perror("recv");
			 	}
			 read_buf[pos + 1] = '\0';
			 
			 *bytes_read = pos;
			}
		while(pos < READ_BUF_SIZE && !disconnect_flag && !std::strstr(read_buf, "-----END CERTIFICATE REQUEST-----"));

return disconnect_flag;
}

int store_CSR(std::string csr_filename, int csr_size)
{
FILE *csr_temp;

csr_temp = fopen(csr_filename.c_str, "w");

if (!csr_temp)
	{
	perror("fopen");
	return 1;
	}

ret = fwrite(read_buf, csr_size, 1, csr_temp);

if (ret != 1)
{
	perror("fwrite");
	fclose(csr_temp);
	remove(csr_filename.c_str);
	return 1;
}

fclose(csr_temp);

return 0;
}

int main(int argc, char *argv[])
{
int socket, client_socket, ret, pos, disconnect_flag, bytes_read, cert_size;
struct sockaddr_in sd, sc;


socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

memset(&sd,0,sizeof(sd));

sd.sin_family = AF_INET;
sd.sin_addr.s_addr = INADDR_ANY;
sd.sin_port = htons(PICA_REGISTRAR_PORT);

ret = bind(socket,(struct sockaddr*)&sd,sizeof(sd));

ret = listen(socket, 60);

while (1)
	{
	memset(&sc, 0, sizeof(sc));

	client_socket = accept(socket, (struct sockaddr*)&sc, sizeof(sc) );

	if (client_socket >= 0)
		{
		
		disconnect_flag = read_CSR(client_socket, &bytes_read);
		
		if (disconnect_flag)
			continue;
		
		{
		FILE *cert;
		
		std::string filename = (string("/tmp/CSR-") + inet_ntoa(sc.sin_addr) );
		
		if (store_CSR(filename, bytes_read))
			continue;
		
		std::string cert_filename = atoi(current_id) + ".pem";
		
		std::string sign_command = "openssl ca -config ca_config.txt  -utf8 -subj /CN=" + atoi(current_id) + "\\#tester -batch -notext -out " + cert_filename + " -in " + csr_temp;
		
		puts(sign_command.c_str);//debug
		
		system(sign_command.c_str);
		
		//get file size
			{
			struct stat st;
			if (stat(cert_filename.c_str(), &st))
				continue;
			
			cert_size = st.st_size;
			}
		
		cert = fopen(cert_filename, "r");
		
		if (!cert)
			{
			 perror("fopen");
			 remove(filename.c_str);
			 remove(cert_filename);
			 continue;
			}
		
		//send
		
		
		current_id++;
		
		remove(filename.c_str);
		}
		
		}
	else
		perror("accept");

	}


return 0;
}