EVP_PKEY *EVP_PKCS82PKEY(PKCS8_PRIV_KEY_INFO *p8)
{
	EVP_PKEY *pkey = NULL;
	ASN1_OBJECT *algoid;
	const EVP_PKEY_ASN1_METHOD *meth;
	char obj_tmp[80];

	if (!PKCS8_pkey_get0(&algoid, NULL, NULL, NULL, p8))
		return NULL;

	if (!(pkey = EVP_PKEY_new())) {
		EVPerr(EVP_F_EVP_PKCS82PKEY,ERR_R_MALLOC_FAILURE);
		return NULL;
	}

	meth = EVP_PKEY_ASN1_find(OBJ_obj2nid(algoid));

	if (meth)
		{
		if (meth->priv_decode)
			{
			if (!meth->priv_decode(pkey, p8))
				{
				EVPerr(EVP_F_EVP_PKCS82PKEY,
					EVP_R_PRIVATE_KEY_DECODE_ERROR);
				goto error;
				}
			}
		else
			{
			EVPerr(EVP_F_EVP_PKCS82PKEY,
					EVP_R_METHOD_NOT_SUPPORTED);
			goto error;
			}
		}
	else
		{
		EVPerr(EVP_F_EVP_PKCS82PKEY, EVP_R_UNSUPPORTED_PRIVATE_KEY_ALGORITHM);
		i2t_ASN1_OBJECT(obj_tmp, 80, algoid);
		ERR_add_error_data(2, "TYPE=", obj_tmp);
		goto error;
		}
	return pkey;

	error:
	EVP_PKEY_free (pkey);
	return NULL;
}