static int do_x509_check(X509 *x, const unsigned char *chk, size_t chklen,
					unsigned int flags, int check_type)
	{
	GENERAL_NAMES *gens = NULL;
	X509_NAME *name = NULL;
	int i;
	int cnid;
	if (check_type == GEN_EMAIL)
		cnid = NID_pkcs9_emailAddress;
	else if (check_type == GEN_DNS)
		cnid = NID_commonName;
	else
		cnid = 0;

	if (chklen == 0)
		chklen = strlen((const char *)chk);

	gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
	if (gens)
		{
		int rv = 0;
		int alt_type;
		if (cnid)
			alt_type = V_ASN1_IA5STRING;
		else
			alt_type = V_ASN1_OCTET_STRING;
		for (i = 0; i < sk_GENERAL_NAME_num(gens); i++)
			{
			GENERAL_NAME *gen;
			ASN1_STRING *cstr;
			gen = sk_GENERAL_NAME_value(gens, i);
			if(gen->type != check_type)
				continue;
			if (check_type == GEN_EMAIL)
				cstr = gen->d.rfc822Name;
			else if (check_type == GEN_DNS)
				cstr = gen->d.dNSName;
			else
				cstr = gen->d.iPAddress;
			if (do_check_string(cstr, alt_type, chk, chklen))
				{
				rv = 1;
				break;
				}
			}
		GENERAL_NAMES_free(gens);
		if (rv)
			return 1;
		if (!(flags & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT) || !cnid)
			return 0;
		}
	i = -1;
	name = X509_get_subject_name(x);
	while((i = X509_NAME_get_index_by_NID(name, cnid, i)) >= 0)
		{
		X509_NAME_ENTRY *ne;
		ASN1_STRING *str;
		ne = X509_NAME_get_entry(name, i);
		str = X509_NAME_ENTRY_get_data(ne);
		if (do_check_string(str, -1, chk, chklen))
			return 1;
		}
	return 0;
	}
static int do_x509_check(X509 *x, const unsigned char *chk, size_t chklen,
					unsigned int flags, int check_type)
	{
	GENERAL_NAMES *gens = NULL;
	X509_NAME *name = NULL;
	int i;
	int cnid;
	if (check_type == GEN_EMAIL)
		cnid = NID_pkcs9_emailAddress;
	else if (check_type == GEN_DNS)
		cnid = NID_commonName;
	else
		cnid = 0;

	if (chklen == 0)
		chklen = strlen((const char *)chk);

	gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
	if (gens)
		{
		int rv = 0;
		int alt_type;
		if (cnid)
			alt_type = V_ASN1_IA5STRING;
		else
			alt_type = V_ASN1_OCTET_STRING;
		for (i = 0; i < sk_GENERAL_NAME_num(gens); i++)
			{
			GENERAL_NAME *gen;
			ASN1_STRING *cstr;
			gen = sk_GENERAL_NAME_value(gens, i);
			if(gen->type != check_type)
				continue;
			if (check_type == GEN_EMAIL)
				cstr = gen->d.rfc822Name;
			else if (check_type == GEN_DNS)
				cstr = gen->d.dNSName;
			else
				cstr = gen->d.iPAddress;
			if (do_check_string(cstr, alt_type, chk, chklen))
				{
				rv = 1;
				break;
				}
			}
		GENERAL_NAMES_free(gens);
		if (rv)
			return 1;
		if (!(flags & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT) || !cnid)
			return 0;
		}
	i = -1;
	name = X509_get_subject_name(x);
	while((i = X509_NAME_get_index_by_NID(name, cnid, i)) >= 0)
		{
		X509_NAME_ENTRY *ne;
		ASN1_STRING *str;
		ne = X509_NAME_get_entry(name, i);
		str = X509_NAME_ENTRY_get_data(ne);
		if (do_check_string(str, -1, chk, chklen))
			return 1;
		}
	return 0;
	}