int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
	{
	X509_PUBKEY *pk=NULL;
	const EVP_PKEY_ASN1_METHOD *meth;

	if (x == NULL) return(0);

	if ((pk=X509_PUBKEY_new()) == NULL) goto error;

	meth = EVP_PKEY_ASN1_find(pkey->type);

	if (meth)
		{
		if (meth->pub_encode)
			{
			if (!meth->pub_encode(pk, pkey))
				{
				X509err(X509_F_X509_PUBKEY_SET,
					X509_R_PUBLIC_KEY_ENCODE_ERROR);
				goto error;
				}
			}
		else
			{
			X509err(X509_F_X509_PUBKEY_SET,
				X509_R_METHOD_NOT_SUPPORTED);
			goto error;
			}
		}
	else
		{
		X509err(X509_F_X509_PUBKEY_SET,X509_R_UNSUPPORTED_ALGORITHM);
		goto error;
		}

	if (*x != NULL)
		X509_PUBKEY_free(*x);

	*x=pk;

	return 1;
error:
	if (pk != NULL) X509_PUBKEY_free(pk);
	return 0;
	}