tools/bpf/bpftool/sign.c

Source file repositories/reference/linux-study-clean/tools/bpf/bpftool/sign.c

File Facts

System
Linux kernel
Corpus path
tools/bpf/bpftool/sign.c
Extension
.c
Size
4636 bytes
Lines
218
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

if (data && (flags & ERR_TXT_STRING)) {
			p_err("OpenSSL %s: %s:%d: %s", buf, file, line, data);
		} else {
			p_err("OpenSSL %s: %s:%d", buf, file, line);
		}
	}
}

#define DISPLAY_OSSL_ERR(cond)				 \
	do {						 \
		bool __cond = (cond);			 \
		if (__cond && ERR_peek_error())		 \
			display_openssl_errors(__LINE__);\
	} while (0)

static EVP_PKEY *read_private_key(const char *pkey_path)
{
	EVP_PKEY *private_key = NULL;
	BIO *b;

	b = BIO_new_file(pkey_path, "rb");
	private_key = PEM_read_bio_PrivateKey(b, NULL, NULL, NULL);
	BIO_free(b);
	DISPLAY_OSSL_ERR(!private_key);
	return private_key;
}

static X509 *read_x509(const char *x509_name)
{
	unsigned char buf[2];
	X509 *x509 = NULL;
	BIO *b;
	int n;

	b = BIO_new_file(x509_name, "rb");
	if (!b)
		goto cleanup;

	/* Look at the first two bytes of the file to determine the encoding */
	n = BIO_read(b, buf, 2);
	if (n != 2)
		goto cleanup;

	if (BIO_reset(b) != 0)
		goto cleanup;

	if (buf[0] == 0x30 && buf[1] >= 0x81 && buf[1] <= 0x84)
		/* Assume raw DER encoded X.509 */
		x509 = d2i_X509_bio(b, NULL);
	else
		/* Assume PEM encoded X.509 */
		x509 = PEM_read_bio_X509(b, NULL, NULL, NULL);

cleanup:
	BIO_free(b);
	DISPLAY_OSSL_ERR(!x509);
	return x509;
}

__u32 register_session_key(const char *key_der_path)
{
	unsigned char *der_buf = NULL;
	X509 *x509 = NULL;
	int key_id = -1;
	int der_len;

	if (!key_der_path)
		return key_id;
	x509 = read_x509(key_der_path);
	if (!x509)
		goto cleanup;
	der_len = i2d_X509(x509, &der_buf);
	if (der_len < 0)
		goto cleanup;
	key_id = syscall(__NR_add_key, "asymmetric", key_der_path, der_buf,
			     (size_t)der_len, KEY_SPEC_SESSION_KEYRING);
cleanup:
	X509_free(x509);
	OPENSSL_free(der_buf);
	DISPLAY_OSSL_ERR(key_id == -1);
	return key_id;
}

int bpftool_prog_sign(struct bpf_load_and_run_opts *opts)
{
	BIO *bd_in = NULL, *bd_out = NULL;
	EVP_PKEY *private_key = NULL;
	CMS_ContentInfo *cms = NULL;
	long actual_sig_len = 0;
	X509 *x509 = NULL;

Annotation

Implementation Notes