2 * FreeRDP: A Remote Desktop Protocol Client
5 * Copyright 2011 Jiten Pathy
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <freerdp/utils/certstore.h>
26 static char cert_dir[] = "freerdp";
27 static char cert_loc[] = "cacert";
28 static char certstore_file[] = "known_hosts";
30 void certstore_create(rdpCertstore* certstore)
32 certstore->fp = fopen((char*)certstore->file, "w+");
34 if (certstore->fp == NULL)
36 printf("certstore_create: error opening [%s] for writing\n", certstore->file);
40 fflush(certstore->fp);
43 void certstore_load(rdpCertstore* certstore)
45 certstore->fp = fopen((char*) certstore->file, "r+");
48 void certstore_open(rdpCertstore* certstore)
50 struct stat stat_info;
52 if (stat((char*) certstore->file, &stat_info) != 0)
53 certstore_create(certstore);
55 certstore_load(certstore);
58 void certstore_close(rdpCertstore* certstore)
60 if (certstore->fp != NULL)
61 fclose(certstore->fp);
64 char* get_local_certloc()
68 struct stat stat_info;
69 home_path = getenv("HOME");
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);
74 if(stat((char*) certloc, &stat_info) != 0)
77 mkdir(certloc, S_IRUSR | S_IWUSR | S_IXUSR);
79 CreateDirectoryA(certloc, 0);
86 void certstore_init(rdpCertstore* certstore)
90 struct stat stat_info;
93 home_path = getenv("HOME");
95 if (home_path == NULL)
97 printf("could not get home path\n");
101 certstore->home = (char*) xstrdup(home_path);
103 certstore->path = (char*) xmalloc(strlen(certstore->home) + strlen("/.") + strlen(cert_dir) + 1);
104 sprintf(certstore->path, "%s/.%s", certstore->home, cert_dir);
106 if (stat(certstore->path, &stat_info) != 0)
109 mkdir(certstore->path, S_IRUSR | S_IWUSR | S_IXUSR);
111 CreateDirectoryA(certstore->path, 0);
113 printf("creating directory %s\n", certstore->path);
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);
120 certstore_open(certstore);
123 rdpCertdata* certdata_new(char* host_name,char* fingerprint)
125 rdpCertdata* certdata;
127 certdata = (rdpCertdata*) xzalloc(sizeof(rdpCertdata));
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);
140 void certdata_free(rdpCertdata* certdata)
144 xfree(certdata->hostname);
145 xfree(certdata->thumbprint);
150 rdpCertstore* certstore_new(rdpCertdata* certdata)
152 rdpCertstore* certstore;
154 certstore = (rdpCertstore*) xzalloc(sizeof(rdpCertstore));
156 if (certstore != NULL)
158 certstore->certdata = certdata;
159 certstore_init(certstore);
165 void certstore_free(rdpCertstore* certstore)
167 if (certstore != NULL)
169 certstore_close(certstore);
170 xfree(certstore->path);
171 xfree(certstore->file);
172 xfree(certstore->home);
173 certdata_free(certstore->certdata);
178 int match_certdata(rdpCertstore* certstore)
185 rdpCertdata* cert_data;
188 cert_data = certstore->certdata;
190 fseek(fp, 0, SEEK_END);
192 fseek(fp, 0, SEEK_SET);
194 data = (char*) xmalloc(size + 1);
195 length = fread(data, size, 1, fp);
198 return certstore->match;
201 pline = strtok(data, "\n");
203 while (pline != NULL)
205 length = strlen(pline);
209 length = strcspn(pline, " \t");
210 pline[length] = '\0';
212 if (strcmp(pline, cert_data->hostname) == 0)
214 pline = &pline[length + 1];
216 if (strcmp(pline, cert_data->thumbprint) == 0)
217 certstore->match = 0;
219 certstore->match = -1;
224 pline = strtok(NULL, "\n");
228 return certstore->match;
231 void print_certdata(rdpCertstore* certstore)
233 fseek(certstore->fp,0,SEEK_END);
234 fprintf(certstore->fp,"%s %s\n",certstore->certdata->hostname,certstore->certdata->thumbprint);