-
Notifications
You must be signed in to change notification settings - Fork 0
/
SEfile-cli.c
executable file
·216 lines (208 loc) · 7.49 KB
/
SEfile-cli.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
/** \file SEfile-cli.c
* \brief In this file you will find the implementation
* of the public function already described in
* \ref SEfile-cli.h
* \authors Daniele Castro
* \date 04/30/2019
*/
#include "SEfile-cli.h"
int main(int argc, char *argv[]) {
int i, e, cmd = -1;
char *opts[6] = {NULL};
for(i = 0; i < argc ; i++) {
for(e = 0; e < 6; e++) {
if(!strcmp(commands[e], argv[i])) cmd = e;
if(!strcmp(options[e], argv[i])) {
opts[e] = argv[i+1];
}
}
}
//DEBUG
// printf("%s\n", commands[cmd]);
// for(i = 0; i < 6; i++) printf("opts[% 2d] = %s\n", i, opts[i]);
switch(cmd) {
case 0:
list(opts[0], opts[4], opts[5]);
break;
case 1:
wrcff(opts[0], opts[4], opts[1], opts[3]);
break;
case 2:
wrcfs(opts[0], opts[4], opts[1], opts[3]);
break;
case 3:
wrffc(opts[0], opts[4], opts[2], opts[3]);
break;
case 4:
wrsfc(opts[0], opts[4], opts[3]);
break;
case 5:
help();
break;
default:
printf("No valid command found.\n\n");
help();
break;
}
return 0;
}
void list(char *peripheral, char *password, char *directory) {
//device opening
se3_disco_it it;
if(peripheral == NULL) it = show_and_choose_devices();
else it = choose_devices(peripheral);
if(password == NULL) {
printf("Enter the device password: ");
char psswrd[32];
scanf("%s", psswrd);
password = psswrd;
fflush(stdin);
}
se3_session s = init_device(password, it);
//list cipher files in the directory
list_cipher_files_in_directory(directory);
//closing device
close_device(&s);
}
void wrcff(char *peripheral, char *password, char *file_path, char *cipher_file_path) {
//device opening
se3_disco_it it;
if(peripheral == NULL) it = show_and_choose_devices();
else it = choose_devices(peripheral);
if(password == NULL) {
printf("Enter the device password: ");
char psswrd[32];
scanf("%s", psswrd);
password = psswrd;
fflush(stdin);
}
se3_session s = init_device(password, it);
//cipher file creation & opening, file opening
FILE *fd = fopen(file_path, "rb");
SEFILE_FHANDLE sefile_file = open_cipher_file(&s, cipher_file_path, SEFILE_WRITE, SEFILE_NEWFILE);
//cipher file writing
//printf("writing cipher file...\n");
write_cipher_file_from_file(&s, fd, &sefile_file);
//closing files
fclose(fd);
secure_close(&sefile_file);
//closing device
close_device(&s);
}
void wrcfs(char *peripheral, char *password, char *string, char *cipher_file_path) {
//device opening
se3_disco_it it;
if(peripheral == NULL) it = show_and_choose_devices();
else it = choose_devices(peripheral);
if(password == NULL) {
printf("Enter the device password: ");
char psswrd[32];
scanf("%s", psswrd);
password = psswrd;
fflush(stdin);
}
se3_session s = init_device(password, it);
//cipher file creation & opening, file opening
SEFILE_FHANDLE sefile_file = open_cipher_file(&s, cipher_file_path, SEFILE_WRITE, SEFILE_NEWFILE);
//cipher file writing
if(string == NULL) {
//printf("Please, input a string: ");
string = (char*)malloc(BUFFER_LENGHT * sizeof(char));
gets(string);
}
//printf("writing cipher file...\n");
write_cipher_file_from_buffer(&s, string, strlen(string), &sefile_file);
//closing files
secure_close(&sefile_file);
//closing device
close_device(&s);
}
void wrffc(char *peripheral, char *password, char *file_path, char *cipher_file_path) {
//device opening
se3_disco_it it;
if(peripheral == NULL) it = show_and_choose_devices();
else it = choose_devices(peripheral);
if(password == NULL) {
printf("Enter the device password: ");
char psswrd[32];
scanf("%s", psswrd);
password = psswrd;
fflush(stdin);
}
se3_session s = init_device(password, it);
//cipher file opening
SEFILE_FHANDLE sefile_file = open_cipher_file(&s, cipher_file_path, SEFILE_READ, SEFILE_OPEN);
FILE *fd = fopen(file_path, "wb+");
//file writing
//printf("writing binary file...\n");
write_binary_file_from_cipher_file(&s, fd, &sefile_file);
//closing files
fclose(fd);
secure_close(&sefile_file);
//closing device
close_device(&s);
}
void wrsfc(char *peripheral, char *password, char *cipher_file_path) {
char string[BUFFER_LENGHT] = {'\0'};
//device opening
se3_disco_it it;
if(peripheral == NULL) it = show_and_choose_devices();
else it = choose_devices(peripheral);
if(password == NULL) {
printf("Enter the device password: ");
char psswrd[32];
scanf("%s", psswrd);
password = psswrd;
fflush(stdin);
}
se3_session s = init_device(password, it);
//cipher file opening
SEFILE_FHANDLE sefile_file = open_cipher_file(&s, cipher_file_path, SEFILE_READ, SEFILE_OPEN);
//file writing
//printf("writing string...\n");
write_buffer_from_cipher_file(&s, string, BUFFER_LENGHT, &sefile_file);
printf("%s\n", string);
//closing files
secure_close(&sefile_file);
//closing device
close_device(&s);
}
void help(void) {
printf("Usage: SEfile-cli command [options]\n");
printf("\n");
printf("SEfile-cli is a commandline tool to manage encrypted files\n");
printf("using SEcube microcontroller as crypto-engine.\n");
printf("\n");
printf("commands:\n");
printf(" list - list decrypted file names of encrypted files\n");
printf(" wrcff - writes a cipher file from a file\n");
printf(" wrcfs - writes a cipher file from a string\n");
printf(" wrffc - writes a file from a cipher one\n");
printf(" wrsfc - writes a string from a cipher file\n");
printf(" --help - shows this message\n");
printf("\n");
printf("options:\n");
printf(" -pe - device drive letter for windows (ex: \"D\") or path for linux\n");
printf(" -i - input file or string\n");
printf(" -o - output file\n");
printf(" -c - input or output cipher file\n");
printf(" -pa - device password\n");
printf(" -d - directory to list\n");
printf("\n");
printf("usage examples:\n");
printf(" SEfile-cli wrcfs -pa test -i \"Hello world!\" -c cipher_file_out.txt\n");
printf(" on Windows: SEfile-cli wrcff -pe D -pa test -i text_file_in.txt -c cipher_file_out.txt\n");
printf(" on Linux: SEfile-cli wrffc -pe /mnt/sdb1 -pa test -o text_file_out.txt -c cipher_file_out.txt\n");
printf("\n");
printf("For all the commands if -pe is not specified all the available\n");
printf("devices will be displayed and one of them must be chosen.\n");
printf("\n");
printf("For all the commands if -pa is not specified a password will be\n");
printf("asked.\n");
printf("\n");
printf("When no string with -i is passed to wrcfs, it will expect one from\n");
printf("the standard input.");
printf("\n");
printf("\n");
printf("Written by Daniele Castro on 04/30/2019.\n");
}