VCOM Linux Driver
Advantech Inc.
Loading...
Searching...
No Matches
vcom_load_sslcfg.h
Go to the documentation of this file.
1static char * nopass = "";
2
3typedef struct {
4 char *password;
5 char *rootca;
6 char *keyfile;
9
10static char * create_cfg_cwd(char * config_file)
11{
12 char *wd;
13 char *wd_end;
14
15 if((wd_end = memrchr(config_file, '/', strlen(config_file)))){
16 int wdlen = wd_end - config_file + 1;
17
18
19 wd = malloc(wdlen + 1);
20 if(wd == 0){
21 printf("can't malloc for chdir\n");
22 return 0;
23 }
24 wd[wdlen] = '\0';
25 memcpy(wd, config_file , wdlen);
26 }else{
27 return 0;
28 }
29
30 return wd;
31}
32
33static int loadconfig(char * filepath, vc_ssl_cfg * cfg)
34{
35 int fd;
36 int filelen;
37 int ret;
38 int tokcount;
39 char * filedata;
40
42 jsmntok_t *tok;
43 _tree_node * rnode;
44
45
46 fd = open(filepath, O_RDONLY);
47// printf("%s fd = %d\n", filepath, fd);
48 if(fd <= 0){
49 //can't open file
50 return -1;
51 }
52
53 filelen = lseek(fd, 0, SEEK_END);
54// printf("filelen = %d\n", filelen);
55
56 lseek(fd, 0, SEEK_SET);
57 filedata = malloc(filelen);
58
59 ret = read(fd, filedata, filelen);
60
61// printf("ret = %d\n", ret);
62
63 jsmn_init(&p);
64
65 tokcount = 2;
66 tok = malloc(sizeof(*tok) * tokcount);
67
68 do{
69 ret = jsmn_parse(&p, filedata, filelen, tok, tokcount);
70 if(ret == JSMN_ERROR_NOMEM){
71 tokcount = tokcount * 2;
72 tok = realloc_it(tok, sizeof(*tok) * tokcount);
73 if(tok == NULL){
74 return -1;
75 }
76 continue;
77 }else if(ret < 0){
78 printf("failed ret = %d\n", ret);
79 }
80 printf("jsmn_parse %d\n", ret);
81 break;
82 }while(1);
83
84// dump(filedata, tok, p.toknext, 0);
85
86 jstreeret result;
87
88 result = js2tree(filedata, tok, p.toknext);
89 //dumptree(result.node, 0);
90
91 close(fd);
92 free(filedata);
93 free(tok);
94
95 if(jstree_read(result.node->r, &rnode, "ssl", "keyfile")!= 2){
96 printf("didn't find keyfile\n");
97 return -1;
98 }
99 printf("found keyfile = %s\n", rnode->data.data);
100
101 cfg->keyfile = rnode->data.data;
102
103 if(jstree_read(result.node->r, &rnode, "ssl", "rootca")!= 2){
104 printf("didn't find rootCA\n");
105 return -1;
106 }
107 printf("found rootca = %s\n", rnode->data.data);
108
109 cfg->rootca = rnode->data.data;
110
111 if(jstree_read(result.node->r, &rnode, "ssl", "password")!= 2){
112 printf("didn't find password\n");
113 cfg->password = nopass;
114 }else{
115 int _len = jstree_string_decode(0, 0, rnode->data.data);
116 cfg->password = malloc(_len + 1);
117 jstree_string_decode(cfg->password, _len + 1, rnode->data.data);
118 }
119
120 printf("found password = %s\n", cfg->password);
121
122 if(jstree_read(result.node->r, &rnode, "ssl", "accept_expired_key")== 2 &&
123 strcmp(rnode->data.data, "enable") == 0){
124 printf("accept expired CA\n");
125 cfg->accept_expired_key = 1;
126 }else {
127 printf("reject expired CA\n");
128 cfg->accept_expired_key = 0;
129 }
130
131 close(fd);
132
133 return 0;
134}
int jsmn_parse(jsmn_parser *parser, const char *js, size_t len, jsmntok_t *tokens, unsigned int num_tokens)
Definition jsmn.c:151
void jsmn_init(jsmn_parser *parser)
Definition jsmn.c:309
@ JSMN_ERROR_NOMEM
Definition jsmn.h:27
int jstree_string_decode(char *out, int outlen, char *data)
Definition jstree.c:64
jstreeret js2tree(const char *js, jsmntok_t *t, size_t count)
Definition jstree.c:327
#define jstree_read(...)
Definition jstree_read.h:146
char * data
Definition jstree.h:5
Definition jstree.h:8
struct _t_treenode * r
Definition jstree.h:11
_treenode_data data
Definition jstree.h:9
Definition jsmn.h:54
unsigned int toknext
Definition jsmn.h:56
Definition jsmn.h:40
Definition jstree.h:31
_tree_node * node
Definition jstree.h:33
Definition vcom_load_sslcfg.h:3
char * rootca
Definition vcom_load_sslcfg.h:5
char * password
Definition vcom_load_sslcfg.h:4
int accept_expired_key
Definition vcom_load_sslcfg.h:7
char * keyfile
Definition vcom_load_sslcfg.h:6