Commit message: None
CVE: CVE-2018-8970
CWE: CWE-295
Vulnerability description: The int_x509_param_set_hosts function in lib/libcrypto/x509/x509_vpm.c in LibreSSL 2.7.0 before 2.7.1 does not support a certain special case of a zero name length, which causes silent omission of hostname verification, and consequently allows man-in-the-middle attackers to spoof servers and obtain sensitive information via a crafted certificate. NOTE: the LibreSSL documentation indicates that this special case is supported, but the BoringSSL documentation does not.

Project: openbsd

X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param, ASN1_OBJECT *policy)
{
	if (!param->policies) {
		param->policies = sk_ASN1_OBJECT_new_null();
		if (!param->policies)
			return 0;
	}
	if (!sk_ASN1_OBJECT_push(param->policies, policy))
		return 0;
	return 1;
}

Project: openbsd

X509_VERIFY_PARAM_add0_table(X509_VERIFY_PARAM *param)
{
	X509_VERIFY_PARAM *ptmp;
	if (!param_table) {
		param_table = sk_X509_VERIFY_PARAM_new(param_cmp);
		if (!param_table)
			return 0;
	} else {
		size_t idx;

		if ((idx = sk_X509_VERIFY_PARAM_find(param_table, param))
		    != -1) {
			ptmp = sk_X509_VERIFY_PARAM_value(param_table,
			    idx);
			X509_VERIFY_PARAM_free(ptmp);
			(void)sk_X509_VERIFY_PARAM_delete(param_table,
			    idx);
		}
	}
	if (!sk_X509_VERIFY_PARAM_push(param_table, param))
		return 0;
	return 1;
}

Project: openbsd

X509_VERIFY_PARAM_add1_host(X509_VERIFY_PARAM *param,
    const char *name, size_t namelen)
{
	return int_x509_param_set_hosts(param->id, ADD_HOST, name, namelen);
}

Project: openbsd

X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param, unsigned long flags)
{
	param->flags &= ~flags;
	return 1;
}

Project: openbsd

X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param)
{
	if (param == NULL)
		return;
	x509_verify_param_zero(param);
	free(param->id);
	free(param);
}

Project: openbsd

X509_VERIFY_PARAM *X509_VERIFY_PARAM_get0(int id)
{
	int num = sizeof(default_table) / sizeof(X509_VERIFY_PARAM);
	if (id < num)
		return default_table + id;
	return sk_X509_VERIFY_PARAM_value(param_table, id - num);
}

Project: openbsd

X509_VERIFY_PARAM_get0_name(const X509_VERIFY_PARAM *param)
{
	return param->name;
}

Project: openbsd

X509_VERIFY_PARAM_get0_peername(X509_VERIFY_PARAM *param)
{
	return param->id->peername;
}

Project: openbsd

X509_VERIFY_PARAM_get_count(void)
{
	int num = sizeof(default_table) / sizeof(X509_VERIFY_PARAM);
	if (param_table)
		num += sk_X509_VERIFY_PARAM_num(param_table);
	return num;
}

Project: openbsd

X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param)
{
	return param->depth;
}

Project: openbsd

X509_VERIFY_PARAM_get_flags(X509_VERIFY_PARAM *param)
{
	return param->flags;
}

Project: openbsd

X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *dest, const X509_VERIFY_PARAM *src)
{
	unsigned long inh_flags;
	int to_default, to_overwrite;
	X509_VERIFY_PARAM_ID *id;

	if (!src)
		return 1;
	id = src->id;
	inh_flags = dest->inh_flags | src->inh_flags;

	if (inh_flags & X509_VP_FLAG_ONCE)
		dest->inh_flags = 0;

	if (inh_flags & X509_VP_FLAG_LOCKED)
		return 1;

	if (inh_flags & X509_VP_FLAG_DEFAULT)
		to_default = 1;
	else
		to_default = 0;

	if (inh_flags & X509_VP_FLAG_OVERWRITE)
		to_overwrite = 1;
	else
		to_overwrite = 0;

	x509_verify_param_copy(purpose, 0);
	x509_verify_param_copy(trust, 0);
	x509_verify_param_copy(depth, -1);

	/* If overwrite or check time not set, copy across */

	if (to_overwrite || !(dest->flags & X509_V_FLAG_USE_CHECK_TIME)) {
		dest->check_time = src->check_time;
		dest->flags &= ~X509_V_FLAG_USE_CHECK_TIME;
		/* Don't need to copy flag: that is done below */
	}

	if (inh_flags & X509_VP_FLAG_RESET_FLAGS)
		dest->flags = 0;

	dest->flags |= src->flags;

	if (test_x509_verify_param_copy(policies, NULL)) {
		if (!X509_VERIFY_PARAM_set1_policies(dest, src->policies))
			return 0;
	}

	/* Copy the host flags if and only if we're copying the host list */
	if (test_x509_verify_param_copy_id(hosts, NULL)) {
		if (dest->id->hosts) {
			string_stack_free(dest->id->hosts);
			dest->id->hosts = NULL;
		}
		if (id->hosts) {
			dest->id->hosts =
			    sk_deep_copy(id->hosts, strdup, str_free);
			if (dest->id->hosts == NULL)
				return 0;
			dest->id->hostflags = id->hostflags;
		}
	}

	if (test_x509_verify_param_copy_id(email, NULL)) {
		if (!X509_VERIFY_PARAM_set1_email(dest, id->email,
		    id->emaillen))
			return 0;
	}

	if (test_x509_verify_param_copy_id(ip, NULL)) {
		if (!X509_VERIFY_PARAM_set1_ip(dest, id->ip, id->iplen))
			return 0;
	}

	return 1;
}

Project: openbsd

X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(const char *name)
{
	X509_VERIFY_PARAM pm;
	unsigned int i, limit;

	pm.name = (char *)name;
	if (param_table) {
		size_t idx;
		if ((idx = sk_X509_VERIFY_PARAM_find(param_table, &pm)) != -1)
			return sk_X509_VERIFY_PARAM_value(param_table, idx);
	}

	limit = sizeof(default_table) / sizeof(X509_VERIFY_PARAM);
	for (i = 0; i < limit; i++) {
		if (strcmp(default_table[i].name, name) == 0) {
			return &default_table[i];
		}
	}
	return NULL;
}

Project: openbsd

X509_VERIFY_PARAM_new(void)
{
	X509_VERIFY_PARAM *param;
	X509_VERIFY_PARAM_ID *paramid;
	param = calloc(1, sizeof(X509_VERIFY_PARAM));
	if (param == NULL)
		return NULL;
	paramid = calloc (1, sizeof(X509_VERIFY_PARAM_ID));
	if (paramid == NULL) {
		free(param);
		return NULL;
	}
	param->id = paramid;
	x509_verify_param_zero(param);
	return param;
}

Project: openbsd

X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to, const X509_VERIFY_PARAM *from)
{
	unsigned long save_flags = to->inh_flags;
	int ret;

	to->inh_flags |= X509_VP_FLAG_DEFAULT;
	ret = X509_VERIFY_PARAM_inherit(to, from);
	to->inh_flags = save_flags;
	return ret;
}

Project: openbsd

X509_VERIFY_PARAM_set1_email(X509_VERIFY_PARAM *param,  const char *email,
    size_t emaillen)
{
	return int_x509_param_set1(&param->id->email, &param->id->emaillen,
	    email, emaillen);
}

Project: openbsd

X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param,
    const char *name, size_t namelen)
{
	return int_x509_param_set_hosts(param->id, SET_HOST, name, namelen);
}

Project: openbsd

X509_VERIFY_PARAM_set1_ip(X509_VERIFY_PARAM *param, const unsigned char *ip,
    size_t iplen)
{
	if (iplen != 0 && iplen != 4 && iplen != 16)
		return 0;
	return int_x509_param_set1((char **)&param->id->ip, &param->id->iplen,
	    (char *)ip, iplen);
}

Project: openbsd

X509_VERIFY_PARAM_set1_ip_asc(X509_VERIFY_PARAM *param, const char *ipasc)
{
	unsigned char ipout[16];
	size_t iplen;

	iplen = (size_t)a2i_ipadd(ipout, ipasc);
	if (iplen == 0)
		return 0;
	return X509_VERIFY_PARAM_set1_ip(param, ipout, iplen);
}

Project: openbsd

X509_VERIFY_PARAM_set1_name(X509_VERIFY_PARAM *param, const char *name)
{
	free(param->name);
	param->name = NULL;
	if (name == NULL)
		return 1;
	param->name = strdup(name);
	if (param->name)
		return 1;
	return 0;
}

Project: openbsd

X509_VERIFY_PARAM_set1_policies(X509_VERIFY_PARAM *param,
    STACK_OF(ASN1_OBJECT) *policies)
{
	int i;
	ASN1_OBJECT *oid, *doid;

	if (!param)
		return 0;
	if (param->policies)
		sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free);

	if (!policies) {
		param->policies = NULL;
		return 1;
	}

	param->policies = sk_ASN1_OBJECT_new_null();
	if (!param->policies)
		return 0;

	for (i = 0; i < sk_ASN1_OBJECT_num(policies); i++) {
		oid = sk_ASN1_OBJECT_value(policies, i);
		doid = OBJ_dup(oid);
		if (!doid)
			return 0;
		if (!sk_ASN1_OBJECT_push(param->policies, doid)) {
			ASN1_OBJECT_free(doid);
			return 0;
		}
	}
	param->flags |= X509_V_FLAG_POLICY_CHECK;
	return 1;
}

Project: openbsd

X509_VERIFY_PARAM_set_hostflags(X509_VERIFY_PARAM *param, unsigned int flags)
{
	param->id->hostflags = flags;
}

Project: openbsd

X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param, int purpose)
{
	return X509_PURPOSE_set(&param->purpose, purpose);
}

Project: openbsd

X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param, time_t t)
{
	param->check_time = t;
	param->flags |= X509_V_FLAG_USE_CHECK_TIME;
}

Project: openbsd

X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param, int trust)
{
	return X509_TRUST_set(&param->trust, trust);
}

Project: openbsd

X509_VERIFY_PARAM_table_cleanup(void)
{
	if (param_table)
		sk_X509_VERIFY_PARAM_pop_free(param_table,
		    X509_VERIFY_PARAM_free);
	param_table = NULL;
}

Project: openbsd

int_x509_param_set1(char **pdest, size_t *pdestlen,  const char *src,
    size_t srclen)
{
	char *tmp;
	if (src) {
		if (srclen == 0) {
			if ((tmp = strdup(src)) == NULL)
				return 0;
			srclen = strlen(src);
		} else {
			if ((tmp = malloc(srclen)) == NULL)
				return 0;
			memcpy(tmp, src, srclen);
		}
	} else {
		tmp = NULL;
		srclen = 0;
	}
	if (*pdest)
		free(*pdest);
	*pdest = tmp;
	if (pdestlen)
		*pdestlen = srclen;
	return 1;
}

Project: openbsd

param_cmp(const X509_VERIFY_PARAM * const *a,
    const X509_VERIFY_PARAM * const *b)
{
	return strcmp((*a)->name, (*b)->name);
}

Project: openbsd

str_free(char *s)
{
    free(s);
}