Blame lib/verify-tofu.c

Packit 549fdc
/*
Packit 549fdc
 * Copyright (C) 2012 Free Software Foundation, Inc.
Packit 549fdc
 *
Packit 549fdc
 * Author: Nikos Mavrogiannopoulos
Packit 549fdc
 *
Packit 549fdc
 * This file is part of GnuTLS.
Packit 549fdc
 *
Packit 549fdc
 * The GnuTLS is free software; you can redistribute it and/or
Packit 549fdc
 * modify it under the terms of the GNU Lesser General Public License
Packit 549fdc
 * as published by the Free Software Foundation; either version 2.1 of
Packit 549fdc
 * the License, or (at your option) any later version.
Packit 549fdc
 *
Packit 549fdc
 * This library is distributed in the hope that it will be useful, but
Packit 549fdc
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 549fdc
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 549fdc
 * Lesser General Public License for more details.
Packit 549fdc
 *
Packit 549fdc
 * You should have received a copy of the GNU Lesser General Public License
Packit 549fdc
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
Packit 549fdc
 *
Packit 549fdc
 */
Packit 549fdc
Packit 549fdc
#include "gnutls_int.h"
Packit 549fdc
#include "errors.h"
Packit 549fdc
#include <libtasn1.h>
Packit 549fdc
#include <global.h>
Packit 549fdc
#include <num.h>		/* MAX */
Packit 549fdc
#include <tls-sig.h>
Packit 549fdc
#include "str.h"
Packit 549fdc
#include <datum.h>
Packit 549fdc
#include "x509_int.h"
Packit 549fdc
#include <nettle/base64.h>
Packit 549fdc
#include <common.h>
Packit 549fdc
#include <gnutls/abstract.h>
Packit 549fdc
#include <system.h>
Packit 549fdc
#include <locks.h>
Packit 549fdc
Packit 549fdc
struct gnutls_tdb_int {
Packit 549fdc
	gnutls_tdb_store_func store;
Packit 549fdc
	gnutls_tdb_store_commitment_func cstore;
Packit 549fdc
	gnutls_tdb_verify_func verify;
Packit 549fdc
};
Packit 549fdc
Packit 549fdc
static int raw_pubkey_to_base64(const gnutls_datum_t * raw,
Packit 549fdc
				gnutls_datum_t * b64);
Packit 549fdc
static int verify_pubkey(const char *file, const char *host,
Packit 549fdc
			 const char *service, const gnutls_datum_t * skey);
Packit 549fdc
Packit 549fdc
static
Packit 549fdc
int store_commitment(const char *db_name, const char *host,
Packit 549fdc
		     const char *service, time_t expiration,
Packit 549fdc
		     gnutls_digest_algorithm_t hash_algo,
Packit 549fdc
		     const gnutls_datum_t * hash);
Packit 549fdc
static
Packit 549fdc
int store_pubkey(const char *db_name, const char *host,
Packit 549fdc
		 const char *service, time_t expiration,
Packit 549fdc
		 const gnutls_datum_t * pubkey);
Packit 549fdc
Packit 549fdc
static int find_config_file(char *file, size_t max_size);
Packit 549fdc
Packit 549fdc
extern void *_gnutls_file_mutex;
Packit 549fdc
Packit 549fdc
struct gnutls_tdb_int default_tdb = {
Packit 549fdc
	store_pubkey,
Packit 549fdc
	store_commitment,
Packit 549fdc
	verify_pubkey
Packit 549fdc
};
Packit 549fdc
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_verify_stored_pubkey:
Packit 549fdc
 * @db_name: A file specifying the stored keys (use NULL for the default)
Packit 549fdc
 * @tdb: A storage structure or NULL to use the default
Packit 549fdc
 * @host: The peer's name
Packit 549fdc
 * @service: non-NULL if this key is specific to a service (e.g. http)
Packit 549fdc
 * @cert_type: The type of the certificate
Packit 549fdc
 * @cert: The raw (der) data of the certificate
Packit 549fdc
 * @flags: should be 0.
Packit 549fdc
 *
Packit 549fdc
 * This function will try to verify the provided (raw or DER-encoded) certificate 
Packit 549fdc
 * using a list of stored public keys.  The @service field if non-NULL should
Packit 549fdc
 * be a port number.
Packit 549fdc
 *
Packit 549fdc
 * The @retrieve variable if non-null specifies a custom backend for
Packit 549fdc
 * the retrieval of entries. If it is NULL then the
Packit 549fdc
 * default file backend will be used. In POSIX-like systems the
Packit 549fdc
 * file backend uses the $HOME/.gnutls/known_hosts file.
Packit 549fdc
 *
Packit 549fdc
 * Note that if the custom storage backend is provided the
Packit 549fdc
 * retrieval function should return %GNUTLS_E_CERTIFICATE_KEY_MISMATCH
Packit 549fdc
 * if the host/service pair is found but key doesn't match,
Packit 549fdc
 * %GNUTLS_E_NO_CERTIFICATE_FOUND if no such host/service with
Packit 549fdc
 * the given key is found, and 0 if it was found. The storage
Packit 549fdc
 * function should return 0 on success.
Packit 549fdc
 *
Packit 549fdc
 * Returns: If no associated public key is found
Packit 549fdc
 * then %GNUTLS_E_NO_CERTIFICATE_FOUND will be returned. If a key
Packit 549fdc
 * is found but does not match %GNUTLS_E_CERTIFICATE_KEY_MISMATCH
Packit 549fdc
 * is returned. On success, %GNUTLS_E_SUCCESS (0) is returned, 
Packit 549fdc
 * or a negative error value on other errors.
Packit 549fdc
 *
Packit 549fdc
 * Since: 3.0.13
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_verify_stored_pubkey(const char *db_name,
Packit 549fdc
			    gnutls_tdb_t tdb,
Packit 549fdc
			    const char *host,
Packit 549fdc
			    const char *service,
Packit 549fdc
			    gnutls_certificate_type_t cert_type,
Packit 549fdc
			    const gnutls_datum_t * cert,
Packit 549fdc
			    unsigned int flags)
Packit 549fdc
{
Packit 549fdc
	gnutls_datum_t pubkey = { NULL, 0 };
Packit 549fdc
	int ret;
Packit 549fdc
	char local_file[MAX_FILENAME];
Packit 549fdc
Packit 549fdc
	if (cert_type != GNUTLS_CRT_X509)
Packit 549fdc
		return
Packit 549fdc
		    gnutls_assert_val
Packit 549fdc
		    (GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE);
Packit 549fdc
Packit 549fdc
	if (db_name == NULL && tdb == NULL) {
Packit 549fdc
		ret = find_config_file(local_file, sizeof(local_file));
Packit 549fdc
		if (ret < 0)
Packit 549fdc
			return gnutls_assert_val(ret);
Packit 549fdc
		db_name = local_file;
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	if (tdb == NULL)
Packit 549fdc
		tdb = &default_tdb;
Packit 549fdc
Packit 549fdc
	ret = x509_raw_crt_to_raw_pubkey(cert, &pubkey);
Packit 549fdc
	if (ret < 0) {
Packit 549fdc
		gnutls_assert();
Packit 549fdc
		goto cleanup;
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	ret = tdb->verify(db_name, host, service, &pubkey);
Packit 549fdc
	if (ret < 0 && ret != GNUTLS_E_CERTIFICATE_KEY_MISMATCH)
Packit 549fdc
		ret = gnutls_assert_val(GNUTLS_E_NO_CERTIFICATE_FOUND);
Packit 549fdc
Packit 549fdc
      cleanup:
Packit 549fdc
	gnutls_free(pubkey.data);
Packit 549fdc
	return ret;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
static int parse_commitment_line(char *line,
Packit 549fdc
				 const char *host, size_t host_len,
Packit 549fdc
				 const char *service, size_t service_len,
Packit 549fdc
				 time_t now, const gnutls_datum_t * skey)
Packit 549fdc
{
Packit 549fdc
	char *p, *kp;
Packit 549fdc
	char *savep = NULL;
Packit 549fdc
	size_t kp_len, phash_size;
Packit 549fdc
	time_t expiration;
Packit 549fdc
	int ret;
Packit 549fdc
	const mac_entry_st *hash_algo;
Packit 549fdc
	uint8_t phash[MAX_HASH_SIZE];
Packit 549fdc
	uint8_t hphash[MAX_HASH_SIZE * 2 + 1];
Packit 549fdc
Packit 549fdc
	/* read host */
Packit 549fdc
	p = strtok_r(line, "|", &savep);
Packit 549fdc
	if (p == NULL)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
Packit 549fdc
Packit 549fdc
	if (p[0] != '*' && host != NULL && strcmp(p, host) != 0)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
Packit 549fdc
Packit 549fdc
	/* read service */
Packit 549fdc
	p = strtok_r(NULL, "|", &savep);
Packit 549fdc
	if (p == NULL)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
Packit 549fdc
Packit 549fdc
	if (p[0] != '*' && service != NULL && strcmp(p, service) != 0)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
Packit 549fdc
Packit 549fdc
	/* read expiration */
Packit 549fdc
	p = strtok_r(NULL, "|", &savep);
Packit 549fdc
	if (p == NULL)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
Packit 549fdc
Packit 549fdc
	expiration = (time_t) atol(p);
Packit 549fdc
	if (expiration > 0 && now > expiration)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_EXPIRED);
Packit 549fdc
Packit 549fdc
	/* read hash algorithm */
Packit 549fdc
	p = strtok_r(NULL, "|", &savep);
Packit 549fdc
	if (p == NULL)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
Packit 549fdc
Packit 549fdc
	hash_algo = mac_to_entry(atol(p));
Packit 549fdc
	if (_gnutls_digest_get_name(hash_algo) == NULL)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
Packit 549fdc
Packit 549fdc
	/* read hash */
Packit 549fdc
	kp = strtok_r(NULL, "|", &savep);
Packit 549fdc
	if (kp == NULL)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
Packit 549fdc
Packit 549fdc
	p = strpbrk(kp, "\n \r\t|");
Packit 549fdc
	if (p != NULL)
Packit 549fdc
		*p = 0;
Packit 549fdc
Packit 549fdc
	/* hash and hex encode */
Packit 549fdc
	ret =
Packit 549fdc
	    _gnutls_hash_fast((gnutls_digest_algorithm_t)hash_algo->id, 
Packit 549fdc
				skey->data, skey->size, phash);
Packit 549fdc
	if (ret < 0)
Packit 549fdc
		return gnutls_assert_val(ret);
Packit 549fdc
Packit 549fdc
	phash_size = _gnutls_hash_get_algo_len(hash_algo);
Packit 549fdc
Packit 549fdc
	p = _gnutls_bin2hex(phash, phash_size, (void *) hphash,
Packit 549fdc
			    sizeof(hphash), NULL);
Packit 549fdc
	if (p == NULL)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
Packit 549fdc
Packit 549fdc
	kp_len = strlen(kp);
Packit 549fdc
	if (kp_len != phash_size * 2)
Packit 549fdc
		return
Packit 549fdc
		    gnutls_assert_val(GNUTLS_E_CERTIFICATE_KEY_MISMATCH);
Packit 549fdc
Packit 549fdc
	if (memcmp(kp, hphash, kp_len) != 0)
Packit 549fdc
		return
Packit 549fdc
		    gnutls_assert_val(GNUTLS_E_CERTIFICATE_KEY_MISMATCH);
Packit 549fdc
Packit 549fdc
	/* key found and matches */
Packit 549fdc
	return 0;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
Packit 549fdc
static int parse_line(char *line,
Packit 549fdc
		      const char *host, size_t host_len,
Packit 549fdc
		      const char *service, size_t service_len,
Packit 549fdc
		      time_t now,
Packit 549fdc
		      const gnutls_datum_t * rawkey,
Packit 549fdc
		      const gnutls_datum_t * b64key)
Packit 549fdc
{
Packit 549fdc
	char *p, *kp;
Packit 549fdc
	char *savep = NULL;
Packit 549fdc
	size_t kp_len;
Packit 549fdc
	time_t expiration;
Packit 549fdc
Packit 549fdc
	/* read version */
Packit 549fdc
	p = strtok_r(line, "|", &savep);
Packit 549fdc
	if (p == NULL)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
Packit 549fdc
Packit 549fdc
	if (strncmp(p, "c0", 2) == 0)
Packit 549fdc
		return parse_commitment_line(p + 3, host, host_len,
Packit 549fdc
					     service, service_len, now,
Packit 549fdc
					     rawkey);
Packit 549fdc
Packit 549fdc
	if (strncmp(p, "g0", 2) != 0)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
Packit 549fdc
Packit 549fdc
	/* read host */
Packit 549fdc
	p = strtok_r(NULL, "|", &savep);
Packit 549fdc
	if (p == NULL)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
Packit 549fdc
Packit 549fdc
	if (p[0] != '*' && host != NULL && strcmp(p, host) != 0)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
Packit 549fdc
Packit 549fdc
	/* read service */
Packit 549fdc
	p = strtok_r(NULL, "|", &savep);
Packit 549fdc
	if (p == NULL)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
Packit 549fdc
Packit 549fdc
	if (p[0] != '*' && service != NULL && strcmp(p, service) != 0)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
Packit 549fdc
Packit 549fdc
	/* read expiration */
Packit 549fdc
	p = strtok_r(NULL, "|", &savep);
Packit 549fdc
	if (p == NULL)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
Packit 549fdc
Packit 549fdc
	expiration = (time_t) atol(p);
Packit 549fdc
	if (expiration > 0 && now > expiration)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_EXPIRED);
Packit 549fdc
Packit 549fdc
	/* read key */
Packit 549fdc
	kp = strtok_r(NULL, "|", &savep);
Packit 549fdc
	if (kp == NULL)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
Packit 549fdc
Packit 549fdc
	p = strpbrk(kp, "\n \r\t|");
Packit 549fdc
	if (p != NULL)
Packit 549fdc
		*p = 0;
Packit 549fdc
Packit 549fdc
	kp_len = strlen(kp);
Packit 549fdc
	if (kp_len != b64key->size)
Packit 549fdc
		return
Packit 549fdc
		    gnutls_assert_val(GNUTLS_E_CERTIFICATE_KEY_MISMATCH);
Packit 549fdc
Packit 549fdc
	if (memcmp(kp, b64key->data, b64key->size) != 0)
Packit 549fdc
		return
Packit 549fdc
		    gnutls_assert_val(GNUTLS_E_CERTIFICATE_KEY_MISMATCH);
Packit 549fdc
Packit 549fdc
	/* key found and matches */
Packit 549fdc
	return 0;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/* Returns the base64 key if found 
Packit 549fdc
 */
Packit 549fdc
static int verify_pubkey(const char *file,
Packit 549fdc
			 const char *host, const char *service,
Packit 549fdc
			 const gnutls_datum_t * pubkey)
Packit 549fdc
{
Packit 549fdc
	FILE *fd;
Packit 549fdc
	char *line = NULL;
Packit 549fdc
	size_t line_size = 0;
Packit 549fdc
	int ret, l2, mismatch = 0;
Packit 549fdc
	size_t host_len = 0, service_len = 0;
Packit 549fdc
	time_t now = gnutls_time(0);
Packit 549fdc
	gnutls_datum_t b64key = { NULL, 0 };
Packit 549fdc
Packit 549fdc
	ret = raw_pubkey_to_base64(pubkey, &b64key);
Packit 549fdc
	if (ret < 0)
Packit 549fdc
		return gnutls_assert_val(ret);
Packit 549fdc
Packit 549fdc
	if (host != NULL)
Packit 549fdc
		host_len = strlen(host);
Packit 549fdc
	if (service != NULL)
Packit 549fdc
		service_len = strlen(service);
Packit 549fdc
Packit 549fdc
	fd = fopen(file, "rb");
Packit 549fdc
	if (fd == NULL) {
Packit 549fdc
		ret = gnutls_assert_val(GNUTLS_E_FILE_ERROR);
Packit 549fdc
		goto cleanup;
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	do {
Packit 549fdc
		l2 = getline(&line, &line_size, fd);
Packit 549fdc
		if (l2 > 0) {
Packit 549fdc
			ret =
Packit 549fdc
			    parse_line(line, host, host_len, service,
Packit 549fdc
				       service_len, now, pubkey, &b64key);
Packit 549fdc
			if (ret == 0) {	/* found */
Packit 549fdc
				goto cleanup;
Packit 549fdc
			} else if (ret ==
Packit 549fdc
				   GNUTLS_E_CERTIFICATE_KEY_MISMATCH)
Packit 549fdc
				mismatch = 1;
Packit 549fdc
		}
Packit 549fdc
	}
Packit 549fdc
	while (l2 >= 0);
Packit 549fdc
Packit 549fdc
	if (mismatch)
Packit 549fdc
		ret = GNUTLS_E_CERTIFICATE_KEY_MISMATCH;
Packit 549fdc
	else
Packit 549fdc
		ret = GNUTLS_E_NO_CERTIFICATE_FOUND;
Packit 549fdc
Packit 549fdc
      cleanup:
Packit 549fdc
	free(line);
Packit 549fdc
	if (fd != NULL)
Packit 549fdc
		fclose(fd);
Packit 549fdc
	gnutls_free(b64key.data);
Packit 549fdc
Packit 549fdc
	return ret;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
static int raw_pubkey_to_base64(const gnutls_datum_t * raw,
Packit 549fdc
				gnutls_datum_t * b64)
Packit 549fdc
{
Packit 549fdc
	size_t size;
Packit 549fdc
Packit 549fdc
	size = BASE64_ENCODE_RAW_LENGTH(raw->size);
Packit 549fdc
Packit 549fdc
	b64->data = gnutls_malloc(size);
Packit 549fdc
	if (b64->data == NULL)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
Packit 549fdc
Packit 549fdc
	base64_encode_raw((void*)b64->data, raw->size, raw->data);
Packit 549fdc
	b64->size = size;
Packit 549fdc
Packit 549fdc
	return 0;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
static
Packit 549fdc
int store_pubkey(const char *db_name, const char *host,
Packit 549fdc
		 const char *service, time_t expiration,
Packit 549fdc
		 const gnutls_datum_t * pubkey)
Packit 549fdc
{
Packit 549fdc
	FILE *fd = NULL;
Packit 549fdc
	gnutls_datum_t b64key = { NULL, 0 };
Packit 549fdc
	int ret;
Packit 549fdc
Packit 549fdc
	ret = gnutls_mutex_lock(&_gnutls_file_mutex);
Packit 549fdc
	if (ret != 0)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_LOCKING_ERROR);
Packit 549fdc
Packit 549fdc
	ret = raw_pubkey_to_base64(pubkey, &b64key);
Packit 549fdc
	if (ret < 0) {
Packit 549fdc
		gnutls_assert();
Packit 549fdc
		goto cleanup;
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	fd = fopen(db_name, "ab+");
Packit 549fdc
	if (fd == NULL) {
Packit 549fdc
		ret = gnutls_assert_val(GNUTLS_E_FILE_ERROR);
Packit 549fdc
		goto cleanup;
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	if (service == NULL)
Packit 549fdc
		service = "*";
Packit 549fdc
	if (host == NULL)
Packit 549fdc
		host = "*";
Packit 549fdc
Packit 549fdc
	fprintf(fd, "|g0|%s|%s|%lu|%.*s\n", host, service,
Packit 549fdc
		(unsigned long) expiration, b64key.size, b64key.data);
Packit 549fdc
Packit 549fdc
	ret = 0;
Packit 549fdc
Packit 549fdc
      cleanup:
Packit 549fdc
	if (fd != NULL)
Packit 549fdc
		fclose(fd);
Packit 549fdc
Packit 549fdc
	gnutls_mutex_unlock(&_gnutls_file_mutex);
Packit 549fdc
	gnutls_free(b64key.data);
Packit 549fdc
Packit 549fdc
	return ret;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
static
Packit 549fdc
int store_commitment(const char *db_name, const char *host,
Packit 549fdc
		     const char *service, time_t expiration,
Packit 549fdc
		     gnutls_digest_algorithm_t hash_algo,
Packit 549fdc
		     const gnutls_datum_t * hash)
Packit 549fdc
{
Packit 549fdc
	FILE *fd;
Packit 549fdc
	char buffer[MAX_HASH_SIZE * 2 + 1];
Packit 549fdc
Packit 549fdc
	fd = fopen(db_name, "ab+");
Packit 549fdc
	if (fd == NULL)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_FILE_ERROR);
Packit 549fdc
Packit 549fdc
	if (service == NULL)
Packit 549fdc
		service = "*";
Packit 549fdc
	if (host == NULL)
Packit 549fdc
		host = "*";
Packit 549fdc
Packit 549fdc
	fprintf(fd, "|c0|%s|%s|%lu|%u|%s\n", host, service,
Packit 549fdc
		(unsigned long) expiration, (unsigned) hash_algo,
Packit 549fdc
		_gnutls_bin2hex(hash->data, hash->size, buffer,
Packit 549fdc
				sizeof(buffer), NULL));
Packit 549fdc
Packit 549fdc
	fclose(fd);
Packit 549fdc
Packit 549fdc
	return 0;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_store_pubkey:
Packit 549fdc
 * @db_name: A file specifying the stored keys (use NULL for the default)
Packit 549fdc
 * @tdb: A storage structure or NULL to use the default
Packit 549fdc
 * @host: The peer's name
Packit 549fdc
 * @service: non-NULL if this key is specific to a service (e.g. http)
Packit 549fdc
 * @cert_type: The type of the certificate
Packit 549fdc
 * @cert: The data of the certificate
Packit 549fdc
 * @expiration: The expiration time (use 0 to disable expiration)
Packit 549fdc
 * @flags: should be 0.
Packit 549fdc
 *
Packit 549fdc
 * This function will store the provided (raw or DER-encoded) certificate to 
Packit 549fdc
 * the list of stored public keys. The key will be considered valid until 
Packit 549fdc
 * the provided expiration time.
Packit 549fdc
 *
Packit 549fdc
 * The @store variable if non-null specifies a custom backend for
Packit 549fdc
 * the storage of entries. If it is NULL then the
Packit 549fdc
 * default file backend will be used.
Packit 549fdc
 *
Packit 549fdc
 * Unless an alternative @tdb is provided, the storage format is a textual format
Packit 549fdc
 * consisting of a line for each host with fields separated by '|'. The contents of
Packit 549fdc
 * the fields are a format-identifier which is set to 'g0', the hostname that the
Packit 549fdc
 * rest of the data applies to, the numeric port or host name, the expiration
Packit 549fdc
 * time in seconds since the epoch (0 for no expiration), and a base64
Packit 549fdc
 * encoding of the raw (DER) public key information (SPKI) of the peer.
Packit 549fdc
 *
Packit 549fdc
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
Packit 549fdc
 *   negative error value.
Packit 549fdc
 *
Packit 549fdc
 * Since: 3.0.13
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_store_pubkey(const char *db_name,
Packit 549fdc
		    gnutls_tdb_t tdb,
Packit 549fdc
		    const char *host,
Packit 549fdc
		    const char *service,
Packit 549fdc
		    gnutls_certificate_type_t cert_type,
Packit 549fdc
		    const gnutls_datum_t * cert,
Packit 549fdc
		    time_t expiration, unsigned int flags)
Packit 549fdc
{
Packit 549fdc
	gnutls_datum_t pubkey = { NULL, 0 };
Packit 549fdc
	int ret;
Packit 549fdc
	char local_file[MAX_FILENAME];
Packit 549fdc
Packit 549fdc
	if (cert_type != GNUTLS_CRT_X509)
Packit 549fdc
		return
Packit 549fdc
		    gnutls_assert_val
Packit 549fdc
		    (GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE);
Packit 549fdc
Packit 549fdc
	if (db_name == NULL && tdb == NULL) {
Packit 549fdc
		ret =
Packit 549fdc
		    _gnutls_find_config_path(local_file,
Packit 549fdc
					     sizeof(local_file));
Packit 549fdc
		if (ret < 0)
Packit 549fdc
			return gnutls_assert_val(ret);
Packit 549fdc
Packit 549fdc
		_gnutls_debug_log("Configuration path: %s\n", local_file);
Packit 549fdc
		mkdir(local_file, 0700);
Packit 549fdc
Packit 549fdc
		ret = find_config_file(local_file, sizeof(local_file));
Packit 549fdc
		if (ret < 0)
Packit 549fdc
			return gnutls_assert_val(ret);
Packit 549fdc
		db_name = local_file;
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	if (tdb == NULL)
Packit 549fdc
		tdb = &default_tdb;
Packit 549fdc
Packit 549fdc
	ret = x509_raw_crt_to_raw_pubkey(cert, &pubkey);
Packit 549fdc
	if (ret < 0) {
Packit 549fdc
		gnutls_assert();
Packit 549fdc
		goto cleanup;
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	_gnutls_debug_log("Configuration file: %s\n", db_name);
Packit 549fdc
Packit 549fdc
	tdb->store(db_name, host, service, expiration, &pubkey);
Packit 549fdc
Packit 549fdc
	ret = 0;
Packit 549fdc
Packit 549fdc
      cleanup:
Packit 549fdc
	gnutls_free(pubkey.data);
Packit 549fdc
Packit 549fdc
	return ret;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_store_commitment:
Packit 549fdc
 * @db_name: A file specifying the stored keys (use NULL for the default)
Packit 549fdc
 * @tdb: A storage structure or NULL to use the default
Packit 549fdc
 * @host: The peer's name
Packit 549fdc
 * @service: non-NULL if this key is specific to a service (e.g. http)
Packit 549fdc
 * @hash_algo: The hash algorithm type
Packit 549fdc
 * @hash: The raw hash
Packit 549fdc
 * @expiration: The expiration time (use 0 to disable expiration)
Packit 549fdc
 * @flags: should be 0 or %GNUTLS_SCOMMIT_FLAG_ALLOW_BROKEN.
Packit 549fdc
 *
Packit 549fdc
 * This function will store the provided hash commitment to 
Packit 549fdc
 * the list of stored public keys. The key with the given
Packit 549fdc
 * hash will be considered valid until the provided expiration time.
Packit 549fdc
 *
Packit 549fdc
 * The @store variable if non-null specifies a custom backend for
Packit 549fdc
 * the storage of entries. If it is NULL then the
Packit 549fdc
 * default file backend will be used.
Packit 549fdc
 *
Packit 549fdc
 * Note that this function is not thread safe with the default backend.
Packit 549fdc
 *
Packit 549fdc
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
Packit 549fdc
 *   negative error value.
Packit 549fdc
 *
Packit 549fdc
 * Since: 3.0
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_store_commitment(const char *db_name,
Packit 549fdc
			gnutls_tdb_t tdb,
Packit 549fdc
			const char *host,
Packit 549fdc
			const char *service,
Packit 549fdc
			gnutls_digest_algorithm_t hash_algo,
Packit 549fdc
			const gnutls_datum_t * hash,
Packit 549fdc
			time_t expiration, unsigned int flags)
Packit 549fdc
{
Packit 549fdc
	int ret;
Packit 549fdc
	char local_file[MAX_FILENAME];
Packit 549fdc
	const mac_entry_st *me = hash_to_entry(hash_algo);
Packit 549fdc
Packit 549fdc
	if (me == NULL)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_ILLEGAL_PARAMETER);
Packit 549fdc
Packit 549fdc
	if (!(flags & GNUTLS_SCOMMIT_FLAG_ALLOW_BROKEN) && _gnutls_digest_is_secure(me) == 0)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_INSUFFICIENT_SECURITY);
Packit 549fdc
Packit 549fdc
	if (_gnutls_hash_get_algo_len(me) != hash->size)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
Packit 549fdc
Packit 549fdc
	if (db_name == NULL && tdb == NULL) {
Packit 549fdc
		ret =
Packit 549fdc
		    _gnutls_find_config_path(local_file,
Packit 549fdc
					     sizeof(local_file));
Packit 549fdc
		if (ret < 0)
Packit 549fdc
			return gnutls_assert_val(ret);
Packit 549fdc
Packit 549fdc
		_gnutls_debug_log("Configuration path: %s\n", local_file);
Packit 549fdc
		mkdir(local_file, 0700);
Packit 549fdc
Packit 549fdc
		ret = find_config_file(local_file, sizeof(local_file));
Packit 549fdc
		if (ret < 0)
Packit 549fdc
			return gnutls_assert_val(ret);
Packit 549fdc
		db_name = local_file;
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	if (tdb == NULL)
Packit 549fdc
		tdb = &default_tdb;
Packit 549fdc
Packit 549fdc
	_gnutls_debug_log("Configuration file: %s\n", db_name);
Packit 549fdc
Packit 549fdc
	tdb->cstore(db_name, host, service, expiration, 
Packit 549fdc
		(gnutls_digest_algorithm_t)me->id, hash);
Packit 549fdc
Packit 549fdc
	ret = 0;
Packit 549fdc
Packit 549fdc
	return ret;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
#define CONFIG_FILE "known_hosts"
Packit 549fdc
Packit 549fdc
static int find_config_file(char *file, size_t max_size)
Packit 549fdc
{
Packit 549fdc
	char path[MAX_FILENAME];
Packit 549fdc
	int ret;
Packit 549fdc
Packit 549fdc
	ret = _gnutls_find_config_path(path, sizeof(path));
Packit 549fdc
	if (ret < 0)
Packit 549fdc
		return gnutls_assert_val(ret);
Packit 549fdc
Packit 549fdc
	if (path[0] == 0)
Packit 549fdc
		snprintf(file, max_size, "%s", CONFIG_FILE);
Packit 549fdc
	else
Packit 549fdc
		snprintf(file, max_size, "%s/%s", path, CONFIG_FILE);
Packit 549fdc
Packit 549fdc
	return 0;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_tdb_init:
Packit 549fdc
 * @tdb: A pointer to the type to be initialized
Packit 549fdc
 *
Packit 549fdc
 * This function will initialize a public key trust storage structure.
Packit 549fdc
 *
Packit 549fdc
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a
Packit 549fdc
 *   negative error value.
Packit 549fdc
 **/
Packit 549fdc
int gnutls_tdb_init(gnutls_tdb_t * tdb)
Packit 549fdc
{
Packit 549fdc
	*tdb = gnutls_calloc(1, sizeof(struct gnutls_tdb_int));
Packit 549fdc
Packit 549fdc
	if (!*tdb)
Packit 549fdc
		return GNUTLS_E_MEMORY_ERROR;
Packit 549fdc
Packit 549fdc
	return 0;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_set_store_func:
Packit 549fdc
 * @tdb: The trust storage
Packit 549fdc
 * @store: The storage function
Packit 549fdc
 *
Packit 549fdc
 * This function will associate a storage function with the
Packit 549fdc
 * trust storage structure. The function is of the following form.
Packit 549fdc
 *
Packit 549fdc
 * int gnutls_tdb_store_func(const char* db_name, const char* host,
Packit 549fdc
 *		       const char* service, time_t expiration,
Packit 549fdc
 *		       const gnutls_datum_t* pubkey);
Packit 549fdc
 *
Packit 549fdc
 * The @db_name should be used to pass any private data to this function.
Packit 549fdc
 *
Packit 549fdc
 **/
Packit 549fdc
void gnutls_tdb_set_store_func(gnutls_tdb_t tdb,
Packit 549fdc
			       gnutls_tdb_store_func store)
Packit 549fdc
{
Packit 549fdc
	tdb->store = store;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_set_store_commitment_func:
Packit 549fdc
 * @tdb: The trust storage
Packit 549fdc
 * @cstore: The commitment storage function
Packit 549fdc
 *
Packit 549fdc
 * This function will associate a commitment (hash) storage function with the
Packit 549fdc
 * trust storage structure. The function is of the following form.
Packit 549fdc
 *
Packit 549fdc
 * int gnutls_tdb_store_commitment_func(const char* db_name, const char* host,
Packit 549fdc
 *		       const char* service, time_t expiration,
Packit 549fdc
 *		       gnutls_digest_algorithm_t, const gnutls_datum_t* hash);
Packit 549fdc
 *
Packit 549fdc
 * The @db_name should be used to pass any private data to this function.
Packit 549fdc
 *
Packit 549fdc
 **/
Packit 549fdc
void gnutls_tdb_set_store_commitment_func(gnutls_tdb_t tdb,
Packit 549fdc
					  gnutls_tdb_store_commitment_func
Packit 549fdc
					  cstore)
Packit 549fdc
{
Packit 549fdc
	tdb->cstore = cstore;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_set_verify_func:
Packit 549fdc
 * @tdb: The trust storage
Packit 549fdc
 * @verify: The verification function
Packit 549fdc
 *
Packit 549fdc
 * This function will associate a retrieval function with the
Packit 549fdc
 * trust storage structure. The function is of the following form.
Packit 549fdc
 *
Packit 549fdc
 * int gnutls_tdb_verify_func(const char* db_name, const char* host,
Packit 549fdc
 *		      const char* service, const gnutls_datum_t* pubkey);
Packit 549fdc
 *
Packit 549fdc
 * The verify function should return zero on a match, %GNUTLS_E_CERTIFICATE_KEY_MISMATCH
Packit 549fdc
 * if there is a mismatch and any other negative error code otherwise.
Packit 549fdc
 *
Packit 549fdc
 * The @db_name should be used to pass any private data to this function.
Packit 549fdc
 *
Packit 549fdc
 **/
Packit 549fdc
void gnutls_tdb_set_verify_func(gnutls_tdb_t tdb,
Packit 549fdc
				gnutls_tdb_verify_func verify)
Packit 549fdc
{
Packit 549fdc
	tdb->verify = verify;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_tdb_deinit:
Packit 549fdc
 * @tdb: The structure to be deinitialized
Packit 549fdc
 *
Packit 549fdc
 * This function will deinitialize a public key trust storage structure.
Packit 549fdc
 **/
Packit 549fdc
void gnutls_tdb_deinit(gnutls_tdb_t tdb)
Packit 549fdc
{
Packit 549fdc
	gnutls_free(tdb);
Packit 549fdc
}