Example #1
0
int
create_public_key_digest(struct ndn_pkey *key,
		PyObject **py_public_key_digest, int *public_key_digest_len)
{
	unsigned int err;
	unsigned char *public_key_der = NULL;
	size_t der_len;
	unsigned char *key_digest;
	size_t key_digest_size;
	PyObject *py_digest = NULL;
	int r;

	assert(key);
	assert(py_public_key_digest);

	r = i2d_PUBKEY((EVP_PKEY *)key, &public_key_der);
	if (r < 0) {
		goto openssl_error;
	}
	der_len = r;

	r = create_key_digest(public_key_der, der_len, &key_digest,
			&key_digest_size);
	free(public_key_der);
	public_key_der = NULL;
	JUMP_IF_NEG(r, error);

	py_digest = PyBytes_FromStringAndSize((char *) key_digest, key_digest_size);
	JUMP_IF_NULL(py_digest, error);

	*py_public_key_digest = py_digest;
	if (public_key_digest_len)
		*public_key_digest_len = key_digest_size;

	return 0;

openssl_error:
	err = ERR_get_error();
	PyErr_Format(g_PyExc_NDNKeyError, "Unable to generate digest from the key:"
			" %s", ERR_reason_error_string(err));
error:
	if (public_key_der)
		free(public_key_der);
	return -1;
}
Example #2
0
int
create_public_key_digest(RSA *private_key_rsa,
		PyObject **py_public_key_digest, int *public_key_digest_len)
{
	unsigned int err;
	unsigned char *public_key_der = NULL;
	size_t der_len;
	unsigned char *key_digest;
	size_t key_digest_size;
	PyObject *py_digest = NULL;
	int r;
	EVP_PKEY *public_key = NULL;
	RSA *public_key_rsa = NULL;

	assert(private_key_rsa);
	assert(py_public_key_digest);

	public_key = EVP_PKEY_new();
	JUMP_IF_NULL(public_key, openssl_error);

	public_key_rsa = RSAPublicKey_dup(private_key_rsa);
	JUMP_IF_NULL(public_key_rsa, openssl_error);

	r = EVP_PKEY_set1_RSA(public_key, public_key_rsa);
	RSA_free(public_key_rsa);
	public_key_rsa = NULL;
	JUMP_IF_NEG(r, openssl_error);

	r = i2d_PUBKEY(public_key, &public_key_der);
	EVP_PKEY_free(public_key);
	public_key = NULL;
	if (r < 0) {
		free(public_key_der);
		goto openssl_error;
	}
	der_len = r;

	r = create_key_digest(public_key_der, der_len, &key_digest,
			&key_digest_size);
	free(public_key_der);
	public_key_der = NULL;
	JUMP_IF_NEG(r, error);

	py_digest = PyBytes_FromStringAndSize((char *) key_digest, key_digest_size);
	JUMP_IF_NULL(py_digest, error);

	*py_public_key_digest = py_digest;
	if (public_key_digest_len)
		*public_key_digest_len = key_digest_size;

	return 0;

openssl_error:
	err = ERR_get_error();
	PyErr_Format(g_PyExc_CCNKeyError, "Unable to generate digest from the key:"
			" %s", ERR_reason_error_string(err));
error:
	if (public_key_rsa)
		RSA_free(public_key_rsa);
	if (public_key)
		EVP_PKEY_free(public_key);
	if (public_key_der)
		free(public_key_der);
	return -1;
}