forked from khu-mesl-348/SecurePi_Secure-FW-update
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Secure_FW_Update_Server.c
251 lines (202 loc) · 4.78 KB
/
Secure_FW_Update_Server.c
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Basic Header
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
// OpenSSL Header
#include <openssl/sha.h>
#include <openssl/rsa.h>
#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
int generate_firmware_signature(unsigned char* sign)
{
FILE* fp = NULL;
// SHA1 Value
int i;
SHA_CTX ctx;
unsigned char buf[256];
char sha1_result[SHA_DIGEST_LENGTH];
// Signature Value
int sign_len;
RSA* priv_key = NULL;
// Hash New Firmware
if (!(fp = fopen("Firmware", "rb")))
{
printf("Firmware Open Fail\n");
return 1;
}
SHA1_Init(&ctx);
while ((i = fread(buf, 1, sizeof(buf), fp)) > 0)
SHA1_Update(&ctx, buf, i);
SHA1_Final(sha1_result, &ctx);
fclose(fp);
// Generate New Firmware Signature
if (!(fp = fopen("private", "rb")))
{
printf("Private Key Open Error\n");
return 1;
}
priv_key = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
if (priv_key == NULL)
{
printf("Read Private Key for RSA Error\n");
return 1;
}
fclose(fp);
sign_len = RSA_private_encrypt(20, (unsigned char*)sha1_result, sign, priv_key, RSA_PKCS1_PADDING);
if (sign_len < 1)
{
printf("RSA private encryption failed\n");
return 1;
}
return 0;
}
int sendData(BIO* sbio, unsigned char* sign)
{
FILE *fp;
int sendLen;
char *sendBuf = NULL;
int bootLen, fwLen, certLen, signLen;
char *bootBuf = NULL, *fwBuf = NULL, *certBuf = NULL;
int len;
char bootlenBuf[10] = "", fwlenBuf[10] = "", certlenBuf[10] = "", signlenBuf[10] = "";
char verBuf[4] = "112";
// Read New Bootloader
if (!(fp = fopen("Boot", "rb")))
{
printf("Boot Open Error\n");
return 1;
}
fseek(fp, 0L, SEEK_END);
bootLen = ftell(fp);
fseek(fp, 0L, SEEK_SET);
bootBuf = (char*)calloc(bootLen, sizeof(char));
fread(bootBuf, 1, bootLen, fp);
fclose(fp);
// Read New Firmware
if (!(fp = fopen("Firmware", "rb")))
{
printf("Firmware Open Error\n");
return 1;
}
fseek(fp, 0L, SEEK_END);
fwLen = ftell(fp);
fseek(fp, 0L, SEEK_SET);
fwBuf = (char*)calloc(fwLen, sizeof(char));
fread(fwBuf, 1, fwLen, fp);
fclose(fp);
// Read Certificate
if (!(fp = fopen("cert", "rb")))
{
printf("Certificate Open Error\n");
return 1;
}
fseek(fp, 0L, SEEK_END);
certLen = ftell(fp);
fseek(fp, 0L, SEEK_SET);
certBuf = (char*)calloc(certLen, sizeof(char));
fread(certBuf, 1, certLen, fp);
fclose(fp);
// Assign sendBuf
len = sprintf(bootlenBuf, "%d", bootLen);
sendLen = len + bootLen;
len = sprintf(fwlenBuf, "%d", fwLen);
sendLen = sendLen + len + fwLen;
len = sprintf(certlenBuf, "%d", certLen);
sendLen = sendLen + len + certLen;
signLen = 256;
len = sprintf(signlenBuf, "%d", signLen);
sendLen = sendLen + len + 256; // 256 is Signature Length
sendBuf = (char*)calloc(sendLen + 13, sizeof(char)); // 11(8+5) are add space length(8) and version length(4) and NULL(1)
strcpy(sendBuf, bootlenBuf);
strcat(sendBuf, " ");
strcat(sendBuf, fwlenBuf);
strcat(sendBuf, " ");
strcat(sendBuf, certlenBuf);
strcat(sendBuf, " ");
strcat(sendBuf, signlenBuf);
strcat(sendBuf, " ");
strcat(sendBuf, verBuf);
strcat(sendBuf, bootBuf);
strcat(sendBuf, fwBuf);
strcat(sendBuf, certBuf);
strcat(sendBuf, sign);
if (BIO_write(sbio, sendBuf, sendLen + 13) < 0)
{
printf("Send New Firmware Fail\n");
free(sendBuf);
return 1;
}
free(bootBuf);
free(fwBuf);
free(certBuf);
free(sendBuf);
return 0;
}
int main()
{
// Signature Value
unsigned char sign[256];
// SSL Value
BIO *bio, *abio, *out;
BIO *bio_err = 0;
SSL_METHOD *meth;
SSL_CTX *ctx;
SSL *ssl;
int res;
// SSL Connection Start
if (!bio_err)
{
SSL_library_init();
SSL_load_error_strings();
bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
}
meth = SSLv23_server_method();
ctx = SSL_CTX_new(meth);
res = SSL_CTX_use_certificate_chain_file(ctx, "cert");
assert(res);
res = SSL_CTX_use_PrivateKey_file(ctx, "private", SSL_FILETYPE_PEM);
assert(res);
res = SSL_CTX_check_private_key(ctx);
assert(res);
bio = BIO_new_ssl(ctx, 0);
BIO_get_ssl(bio, &ssl);
assert(ssl);
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
abio = BIO_new_accept("4000");
BIO_set_accept_bios(abio, bio);
if (BIO_do_accept(abio) <= 0)
{
fprintf(stderr, "Error setting up accept BIO\n");
ERR_print_errors_fp(stderr);
return 0;
}
if (BIO_do_accept(abio) <= 0)
{
fprintf(stderr, "Error in connection\n");
ERR_print_errors_fp(stderr);
return 0;
}
out = BIO_pop(abio);
if (BIO_do_handshake(out) <= 0)
{
fprintf(stderr, "Error in SSL handshake\n");
ERR_print_errors_fp(stderr);
return 0;
}
else
printf("SSL Connection Success\n");
// Generate New Firmware Signature
memset(sign, 0, 256);
if (generate_firmware_signature(sign) != 0)
{
printf("New Firmware Signature Generation Fail\n");
return 1;
}
// Send New Firmware and Signature
sendData(out, sign);
BIO_free(bio);
BIO_free(abio);
BIO_free(out);
return 0;
}