2012-02-17 09:58:30 +04:00
|
|
|
/**
|
2012-02-21 09:56:55 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2012-02-17 09:58:30 +04:00
|
|
|
* Certificate Handling
|
|
|
|
*
|
|
|
|
* Copyright 2011 Jiten Pathy
|
2012-02-21 09:56:55 +04:00
|
|
|
* Copyright 2011-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
2012-02-17 09:58:30 +04:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-02-17 09:58:30 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2012-10-09 07:42:01 +04:00
|
|
|
#include <winpr/crt.h>
|
2013-03-22 23:52:43 +04:00
|
|
|
#include <winpr/file.h>
|
|
|
|
#include <winpr/path.h>
|
2012-10-09 07:42:01 +04:00
|
|
|
|
2012-02-17 09:58:30 +04:00
|
|
|
#include <openssl/pem.h>
|
|
|
|
#include <openssl/rsa.h>
|
|
|
|
|
|
|
|
static const char certificate_store_dir[] = "certs";
|
2013-06-07 00:45:19 +04:00
|
|
|
static const char certificate_server_dir[] = "server";
|
2012-02-17 09:58:30 +04:00
|
|
|
static const char certificate_known_hosts_file[] = "known_hosts";
|
|
|
|
|
|
|
|
#include <freerdp/crypto/certificate.h>
|
|
|
|
|
|
|
|
void certificate_store_init(rdpCertificateStore* certificate_store)
|
|
|
|
{
|
2013-06-07 00:45:19 +04:00
|
|
|
char* server_path;
|
2012-02-17 09:58:30 +04:00
|
|
|
rdpSettings* settings;
|
|
|
|
|
|
|
|
settings = certificate_store->settings;
|
|
|
|
|
2013-03-28 04:10:18 +04:00
|
|
|
if (!PathFileExistsA(settings->ConfigPath))
|
|
|
|
{
|
|
|
|
CreateDirectoryA(settings->ConfigPath, 0);
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "creating directory %s\n", settings->ConfigPath);
|
2013-03-28 04:10:18 +04:00
|
|
|
}
|
|
|
|
|
2013-03-22 23:52:43 +04:00
|
|
|
certificate_store->path = GetCombinedPath(settings->ConfigPath, (char*) certificate_store_dir);
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2013-03-28 04:10:18 +04:00
|
|
|
if (!PathFileExistsA(certificate_store->path))
|
2012-02-17 09:58:30 +04:00
|
|
|
{
|
2013-03-22 23:52:43 +04:00
|
|
|
CreateDirectoryA(certificate_store->path, 0);
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "creating directory %s\n", certificate_store->path);
|
2012-02-17 09:58:30 +04:00
|
|
|
}
|
|
|
|
|
2013-06-07 00:45:19 +04:00
|
|
|
server_path = GetCombinedPath(settings->ConfigPath, (char*) certificate_server_dir);
|
|
|
|
|
|
|
|
if (!PathFileExistsA(server_path))
|
|
|
|
{
|
|
|
|
CreateDirectoryA(server_path, 0);
|
|
|
|
fprintf(stderr, "creating directory %s\n", server_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(server_path);
|
|
|
|
|
2013-03-22 23:52:43 +04:00
|
|
|
certificate_store->file = GetCombinedPath(settings->ConfigPath, (char*) certificate_known_hosts_file);
|
2012-02-17 09:58:30 +04:00
|
|
|
|
2013-03-22 08:42:09 +04:00
|
|
|
if (PathFileExistsA(certificate_store->file) == FALSE)
|
2012-02-17 09:58:30 +04:00
|
|
|
{
|
|
|
|
certificate_store->fp = fopen((char*) certificate_store->file, "w+");
|
|
|
|
|
|
|
|
if (certificate_store->fp == NULL)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "certificate_store_open: error opening [%s] for writing\n", certificate_store->file);
|
2012-02-17 09:58:30 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fflush(certificate_store->fp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
certificate_store->fp = fopen((char*) certificate_store->file, "r+");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int certificate_data_match(rdpCertificateStore* certificate_store, rdpCertificateData* certificate_data)
|
|
|
|
{
|
|
|
|
FILE* fp;
|
|
|
|
int length;
|
|
|
|
char* data;
|
|
|
|
char* pline;
|
|
|
|
int match = 1;
|
|
|
|
long int size;
|
|
|
|
|
|
|
|
fp = certificate_store->fp;
|
|
|
|
|
|
|
|
if (!fp)
|
|
|
|
return match;
|
|
|
|
|
|
|
|
fseek(fp, 0, SEEK_END);
|
|
|
|
size = ftell(fp);
|
|
|
|
fseek(fp, 0, SEEK_SET);
|
|
|
|
|
|
|
|
if (size < 1)
|
|
|
|
return match;
|
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
data = (char*) malloc(size + 2);
|
2012-02-17 09:58:30 +04:00
|
|
|
|
|
|
|
if (fread(data, size, 1, fp) != 1)
|
|
|
|
{
|
2012-10-09 07:21:26 +04:00
|
|
|
free(data);
|
2012-02-17 09:58:30 +04:00
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
|
|
|
data[size] = '\n';
|
|
|
|
data[size + 1] = '\0';
|
|
|
|
pline = strtok(data, "\n");
|
|
|
|
|
|
|
|
while (pline != NULL)
|
|
|
|
{
|
|
|
|
length = strlen(pline);
|
|
|
|
|
|
|
|
if (length > 0)
|
|
|
|
{
|
|
|
|
length = strcspn(pline, " \t");
|
|
|
|
pline[length] = '\0';
|
|
|
|
|
|
|
|
if (strcmp(pline, certificate_data->hostname) == 0)
|
|
|
|
{
|
|
|
|
pline = &pline[length + 1];
|
|
|
|
|
|
|
|
if (strcmp(pline, certificate_data->fingerprint) == 0)
|
|
|
|
match = 0;
|
|
|
|
else
|
|
|
|
match = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pline = strtok(NULL, "\n");
|
|
|
|
}
|
2012-10-09 07:21:26 +04:00
|
|
|
free(data);
|
2012-02-17 09:58:30 +04:00
|
|
|
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
2012-06-29 05:05:10 +04:00
|
|
|
void certificate_data_replace(rdpCertificateStore* certificate_store, rdpCertificateData* certificate_data)
|
|
|
|
{
|
|
|
|
FILE* fp;
|
|
|
|
int length;
|
|
|
|
char* data;
|
|
|
|
char* pline;
|
|
|
|
long int size;
|
|
|
|
|
|
|
|
fp = certificate_store->fp;
|
|
|
|
|
|
|
|
if (!fp)
|
|
|
|
return;
|
|
|
|
|
2012-11-13 20:06:33 +04:00
|
|
|
/* Read the current contents of the file. */
|
2012-06-29 05:05:10 +04:00
|
|
|
fseek(fp, 0, SEEK_END);
|
|
|
|
size = ftell(fp);
|
|
|
|
fseek(fp, 0, SEEK_SET);
|
|
|
|
|
|
|
|
if (size < 1)
|
|
|
|
return;
|
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
data = (char*) malloc(size + 2);
|
2012-06-29 05:05:10 +04:00
|
|
|
|
|
|
|
if (fread(data, size, 1, fp) != 1)
|
|
|
|
{
|
2012-10-09 07:21:26 +04:00
|
|
|
free(data);
|
2012-06-29 05:05:10 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-13 20:06:33 +04:00
|
|
|
/* Write the file back out, with appropriate fingerprint substitutions */
|
2012-06-29 05:05:10 +04:00
|
|
|
fp = fopen(certificate_store->file, "w+");
|
|
|
|
data[size] = '\n';
|
|
|
|
data[size + 1] = '\0';
|
|
|
|
pline = strtok(data, "\n"); // xxx: use strsep
|
|
|
|
|
|
|
|
while (pline != NULL)
|
|
|
|
{
|
|
|
|
length = strlen(pline);
|
|
|
|
|
|
|
|
if (length > 0)
|
|
|
|
{
|
|
|
|
char* hostname = pline, *fingerprint;
|
|
|
|
|
|
|
|
length = strcspn(pline, " \t");
|
|
|
|
hostname[length] = '\0';
|
|
|
|
|
|
|
|
/* If this is the replaced hostname, use the updated fingerprint. */
|
|
|
|
if (strcmp(hostname, certificate_data->hostname) == 0)
|
|
|
|
fingerprint = certificate_data->fingerprint;
|
|
|
|
else
|
|
|
|
fingerprint = &hostname[length + 1];
|
|
|
|
|
|
|
|
fprintf(fp, "%s %s\n", hostname, fingerprint);
|
|
|
|
}
|
|
|
|
|
|
|
|
pline = strtok(NULL, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
2012-10-09 07:21:26 +04:00
|
|
|
free(data);
|
2012-06-29 05:05:10 +04:00
|
|
|
}
|
|
|
|
|
2012-02-17 09:58:30 +04:00
|
|
|
void certificate_data_print(rdpCertificateStore* certificate_store, rdpCertificateData* certificate_data)
|
|
|
|
{
|
|
|
|
FILE* fp;
|
|
|
|
|
|
|
|
/* reopen in append mode */
|
|
|
|
fp = fopen(certificate_store->file, "a");
|
|
|
|
|
|
|
|
if (!fp)
|
|
|
|
return;
|
|
|
|
|
|
|
|
fprintf(fp, "%s %s\n", certificate_data->hostname, certificate_data->fingerprint);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
rdpCertificateData* certificate_data_new(char* hostname, char* fingerprint)
|
|
|
|
{
|
|
|
|
rdpCertificateData* certdata;
|
|
|
|
|
2012-11-22 04:22:41 +04:00
|
|
|
certdata = (rdpCertificateData*) malloc(sizeof(rdpCertificateData));
|
2012-02-17 09:58:30 +04:00
|
|
|
|
|
|
|
if (certdata != NULL)
|
|
|
|
{
|
2012-11-22 04:22:41 +04:00
|
|
|
ZeroMemory(certdata, sizeof(rdpCertificateData));
|
|
|
|
|
2012-10-09 07:42:01 +04:00
|
|
|
certdata->hostname = _strdup(hostname);
|
|
|
|
certdata->fingerprint = _strdup(fingerprint);
|
2012-02-17 09:58:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return certdata;
|
|
|
|
}
|
|
|
|
|
|
|
|
void certificate_data_free(rdpCertificateData* certificate_data)
|
|
|
|
{
|
|
|
|
if (certificate_data != NULL)
|
|
|
|
{
|
2012-10-09 07:21:26 +04:00
|
|
|
free(certificate_data->hostname);
|
|
|
|
free(certificate_data->fingerprint);
|
|
|
|
free(certificate_data);
|
2012-02-17 09:58:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rdpCertificateStore* certificate_store_new(rdpSettings* settings)
|
|
|
|
{
|
|
|
|
rdpCertificateStore* certificate_store;
|
|
|
|
|
2012-11-22 04:22:41 +04:00
|
|
|
certificate_store = (rdpCertificateStore*) malloc(sizeof(rdpCertificateStore));
|
2012-02-17 09:58:30 +04:00
|
|
|
|
|
|
|
if (certificate_store != NULL)
|
|
|
|
{
|
2012-11-22 04:22:41 +04:00
|
|
|
ZeroMemory(certificate_store, sizeof(rdpCertificateStore));
|
|
|
|
|
2012-02-17 09:58:30 +04:00
|
|
|
certificate_store->settings = settings;
|
|
|
|
certificate_store_init(certificate_store);
|
|
|
|
}
|
|
|
|
|
|
|
|
return certificate_store;
|
|
|
|
}
|
|
|
|
|
|
|
|
void certificate_store_free(rdpCertificateStore* certstore)
|
|
|
|
{
|
|
|
|
if (certstore != NULL)
|
|
|
|
{
|
|
|
|
if (certstore->fp != NULL)
|
|
|
|
fclose(certstore->fp);
|
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(certstore->path);
|
|
|
|
free(certstore->file);
|
|
|
|
free(certstore);
|
2012-02-17 09:58:30 +04:00
|
|
|
}
|
|
|
|
}
|