wfreerdp: start Win32 GDI implementation, fix certstore
[platform/upstream/freerdp.git] / libfreerdp-utils / certstore.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * certstore Utils
4  *
5  * Copyright 2011 Jiten Pathy
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *               http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <freerdp/utils/certstore.h>
21
22 #ifdef _WIN32
23 #include <windows.h>
24 #endif
25
26 static char cert_dir[] = "freerdp";
27 static char cert_loc[] = "cacert";
28 static char certstore_file[] = "known_hosts";
29
30 void certstore_create(rdpCertstore* certstore)
31 {
32         certstore->fp = fopen((char*)certstore->file, "w+");
33
34         if (certstore->fp == NULL)
35         {
36                 printf("certstore_create: error opening [%s] for writing\n", certstore->file);
37                 return;
38         }
39
40         fflush(certstore->fp);
41 }
42
43 void certstore_load(rdpCertstore* certstore)
44 {
45         certstore->fp = fopen((char*) certstore->file, "r+");
46 }
47
48 void certstore_open(rdpCertstore* certstore)
49 {
50         struct stat stat_info;
51
52         if (stat((char*) certstore->file, &stat_info) != 0)
53                 certstore_create(certstore);
54         else
55                 certstore_load(certstore);
56 }
57
58 void certstore_close(rdpCertstore* certstore)
59 {
60         if (certstore->fp != NULL)
61                 fclose(certstore->fp);
62 }
63
64 char* get_local_certloc()
65 {
66         char* home_path;
67         char* certloc;
68         struct stat stat_info;
69         home_path = getenv("HOME");
70
71         certloc = (char*) xmalloc(strlen(home_path) + strlen("/.") + strlen(cert_dir) + strlen("/") + strlen(cert_loc) + 1);
72         sprintf(certloc,"%s/.%s/%s",home_path,cert_dir,cert_loc);
73
74         if(stat((char*) certloc, &stat_info) != 0)
75         {
76 #ifndef _WIN32
77                 mkdir(certloc, S_IRUSR | S_IWUSR | S_IXUSR);
78 #else
79                 CreateDirectoryA(certloc, 0);
80 #endif
81         }
82         
83         return certloc;
84 }
85
86 void certstore_init(rdpCertstore* certstore)
87 {
88         int length;
89         char* home_path;
90         struct stat stat_info;
91         
92         certstore->match=1;
93         home_path = getenv("HOME");
94
95         if (home_path == NULL)
96         {
97                 printf("could not get home path\n");
98                 return;
99         }
100
101         certstore->home = (char*) xstrdup(home_path);
102
103         certstore->path = (char*) xmalloc(strlen(certstore->home) + strlen("/.") + strlen(cert_dir) + 1);
104         sprintf(certstore->path, "%s/.%s", certstore->home, cert_dir);
105
106         if (stat(certstore->path, &stat_info) != 0)
107         {
108 #ifndef _WIN32
109                 mkdir(certstore->path, S_IRUSR | S_IWUSR | S_IXUSR);
110 #else
111                 CreateDirectoryA(certstore->path, 0);
112 #endif
113                 printf("creating directory %s\n", certstore->path);
114         }
115
116         length = strlen(certstore->path);
117         certstore->file = (char*) xmalloc(strlen(certstore->path) + strlen("/") + strlen(certstore_file) + 1);
118         sprintf(certstore->file, "%s/%s", certstore->path, certstore_file);
119
120         certstore_open(certstore);
121 }
122
123 rdpCertdata* certdata_new(char* host_name,char* fingerprint)
124 {
125         rdpCertdata* certdata;
126
127         certdata = (rdpCertdata*) xzalloc(sizeof(rdpCertdata));
128
129         if (certdata !=NULL)
130         {
131                 certdata->hostname = xzalloc(strlen(host_name) + 1);
132                 certdata->thumbprint = xzalloc(strlen(fingerprint) + 1);
133                 sprintf(certdata->hostname, "%s", host_name);
134                 sprintf(certdata->thumbprint, "%s", fingerprint);
135         }
136
137         return certdata;
138 }
139
140 void certdata_free(rdpCertdata* certdata)
141 {
142         if(certdata != NULL)
143         {
144                 xfree(certdata->hostname);
145                 xfree(certdata->thumbprint);
146                 xfree(certdata);
147         }
148 }
149
150 rdpCertstore* certstore_new(rdpCertdata* certdata)
151 {
152         rdpCertstore* certstore;
153
154         certstore = (rdpCertstore*) xzalloc(sizeof(rdpCertstore));
155
156         if (certstore != NULL)
157         {
158                 certstore->certdata = certdata;
159                 certstore_init(certstore);
160         }
161
162         return certstore;
163 }
164
165 void certstore_free(rdpCertstore* certstore)
166 {
167         if (certstore != NULL)
168         {
169                 certstore_close(certstore);
170                 xfree(certstore->path);
171                 xfree(certstore->file);
172                 xfree(certstore->home);
173                 certdata_free(certstore->certdata);
174                 xfree(certstore);
175         }
176 }
177
178 int match_certdata(rdpCertstore* certstore)
179 {
180         FILE* fp;
181         int length;
182         char* data;
183         char* pline;
184         long int size;
185         rdpCertdata* cert_data;
186
187         fp = certstore->fp;
188         cert_data = certstore->certdata;
189
190         fseek(fp, 0, SEEK_END);
191         size = ftell(fp);
192         fseek(fp, 0, SEEK_SET);
193
194         data = (char*) xmalloc(size + 1);
195         length = fread(data, size, 1, fp);
196
197         if (size < 1)
198                 return certstore->match;
199
200         data[size] = '\n';
201         pline = strtok(data, "\n");
202
203         while (pline != NULL)
204         {
205                 length = strlen(pline);
206
207                 if (length > 0)
208                 {
209                         length = strcspn(pline, " \t");
210                         pline[length] = '\0';
211
212                         if (strcmp(pline, cert_data->hostname) == 0)
213                         {
214                                 pline = &pline[length + 1];
215
216                                 if (strcmp(pline, cert_data->thumbprint) == 0)
217                                         certstore->match = 0;
218                                 else
219                                         certstore->match = -1;
220                                 break;
221                         }
222                 }
223
224                 pline = strtok(NULL, "\n");
225         }
226         xfree(data);
227
228         return certstore->match;
229 }
230
231 void print_certdata(rdpCertstore* certstore)
232 {
233         fseek(certstore->fp,0,SEEK_END);
234         fprintf(certstore->fp,"%s %s\n",certstore->certdata->hostname,certstore->certdata->thumbprint);
235 }