int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,
							unsigned long flags)
	{
	int rv, i, sign_nid;
	EVP_PKEY *pk = NULL;
	unsigned long tflags;
	if (!(flags & X509_V_FLAG_SUITEB_128_LOS))
		return X509_V_OK;
	tflags = flags;
	if (x == NULL)
		{
		x = sk_X509_value(chain, 0);
		i = 1;
		}
	else
		i = 0;
	if (X509_get_version(x) != 2)
		{
		rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
		i = 0;
		goto end;
		}
	pk = X509_get_pubkey(x);
	rv = check_suite_b(pk, -1, &tflags);
	if (rv != X509_V_OK)
		{
		i = 0;
		goto end;
		}
	for(; i < sk_X509_num(chain); i++)
		{
		sign_nid = X509_get_signature_nid(x);
		x = sk_X509_value(chain, i);
		if (X509_get_version(x) != 2)
			{
			rv = X509_V_ERR_SUITE_B_INVALID_VERSION;
			goto end;
			}
		EVP_PKEY_free(pk);
		pk = X509_get_pubkey(x);
		rv = check_suite_b(pk, sign_nid, &tflags);
		if (rv != X509_V_OK)
			goto end;
		}
	rv = check_suite_b(pk, X509_get_signature_nid(x), &tflags);
	end:
	if (pk)
		EVP_PKEY_free(pk);
	if (rv != X509_V_OK)
		{
		if ((rv == X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM
		    || rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED) && i)
			i--;
		if (rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED && flags != tflags)
			rv = X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256;
		if (perror_depth)
			*perror_depth = i;
		}
	return rv;
	}
['int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain,\n\t\t\t\t\t\t\tunsigned long flags)\n\t{\n\tint rv, i, sign_nid;\n\tEVP_PKEY *pk = NULL;\n\tunsigned long tflags;\n\tif (!(flags & X509_V_FLAG_SUITEB_128_LOS))\n\t\treturn X509_V_OK;\n\ttflags = flags;\n\tif (x == NULL)\n\t\t{\n\t\tx = sk_X509_value(chain, 0);\n\t\ti = 1;\n\t\t}\n\telse\n\t\ti = 0;\n\tif (X509_get_version(x) != 2)\n\t\t{\n\t\trv = X509_V_ERR_SUITE_B_INVALID_VERSION;\n\t\ti = 0;\n\t\tgoto end;\n\t\t}\n\tpk = X509_get_pubkey(x);\n\trv = check_suite_b(pk, -1, &tflags);\n\tif (rv != X509_V_OK)\n\t\t{\n\t\ti = 0;\n\t\tgoto end;\n\t\t}\n\tfor(; i < sk_X509_num(chain); i++)\n\t\t{\n\t\tsign_nid = X509_get_signature_nid(x);\n\t\tx = sk_X509_value(chain, i);\n\t\tif (X509_get_version(x) != 2)\n\t\t\t{\n\t\t\trv = X509_V_ERR_SUITE_B_INVALID_VERSION;\n\t\t\tgoto end;\n\t\t\t}\n\t\tEVP_PKEY_free(pk);\n\t\tpk = X509_get_pubkey(x);\n\t\trv = check_suite_b(pk, sign_nid, &tflags);\n\t\tif (rv != X509_V_OK)\n\t\t\tgoto end;\n\t\t}\n\trv = check_suite_b(pk, X509_get_signature_nid(x), &tflags);\n\tend:\n\tif (pk)\n\t\tEVP_PKEY_free(pk);\n\tif (rv != X509_V_OK)\n\t\t{\n\t\tif ((rv == X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM\n\t\t    || rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED) && i)\n\t\t\ti--;\n\t\tif (rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED && flags != tflags)\n\t\t\trv = X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256;\n\t\tif (perror_depth)\n\t\t\t*perror_depth = i;\n\t\t}\n\treturn rv;\n\t}', 'void *sk_value(const _STACK *st, int i)\n{\n\tif(!st || (i < 0) || (i >= st->num)) return NULL;\n\treturn st->data[i];\n}']