Blame src/imaevm.h

Packit c6d22b
/*
Packit c6d22b
 * ima-evm-utils - IMA/EVM support utilities
Packit c6d22b
 *
Packit c6d22b
 * Copyright (C) 2011 Nokia Corporation
Packit c6d22b
 * Copyright (C) 2011,2012,2013 Intel Corporation
Packit c6d22b
 * Copyright (C) 2013,2014 Samsung Electronics
Packit c6d22b
 *
Packit c6d22b
 * Authors:
Packit c6d22b
 * Dmitry Kasatkin <dmitry.kasatkin@nokia.com>
Packit c6d22b
 *                 <dmitry.kasatkin@intel.com>
Packit c6d22b
 *                 <d.kasatkin@samsung.com>
Packit c6d22b
 *
Packit c6d22b
 * This program is free software; you can redistribute it and/or
Packit c6d22b
 * modify it under the terms of the GNU General Public License
Packit c6d22b
 * version 2 as published by the Free Software Foundation.
Packit c6d22b
 *
Packit c6d22b
 * This program is distributed in the hope that it will be useful,
Packit c6d22b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit c6d22b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit c6d22b
 * GNU General Public License for more details.
Packit c6d22b
 *
Packit c6d22b
 * You should have received a copy of the GNU General Public License
Packit c6d22b
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit c6d22b
 *
Packit c6d22b
 * As a special exception, the copyright holders give permission to link the
Packit c6d22b
 * code of portions of this program with the OpenSSL library under certain
Packit c6d22b
 * conditions as described in each individual source file and distribute
Packit c6d22b
 * linked combinations including the program with the OpenSSL library. You
Packit c6d22b
 * must comply with the GNU General Public License in all respects
Packit c6d22b
 * for all of the code used other than as permitted herein. If you modify
Packit c6d22b
 * file(s) with this exception, you may extend this exception to your
Packit c6d22b
 * version of the file(s), but you are not obligated to do so. If you do not
Packit c6d22b
 * wish to do so, delete this exception statement from your version. If you
Packit c6d22b
 * delete this exception statement from all source files in the program,
Packit c6d22b
 * then also delete it in the license file.
Packit c6d22b
 *
Packit c6d22b
 * File: imaevm.h
Packit c6d22b
 *	 IMA/EVM header file
Packit c6d22b
 */
Packit c6d22b
Packit c6d22b
#ifndef _LIBIMAEVM_H
Packit c6d22b
#define _LIBIMAEVM_H
Packit c6d22b
Packit c6d22b
#include <linux/fs.h>
Packit c6d22b
#include <stdint.h>
Packit c6d22b
#include <syslog.h>
Packit c6d22b
#include <stdbool.h>
Packit c6d22b
#include <errno.h>
Packit c6d22b
Packit c6d22b
#include <openssl/rsa.h>
Packit c6d22b
Packit c6d22b
#ifdef USE_FPRINTF
Packit c6d22b
#define do_log(level, fmt, args...)	({ if (level <= params.verbose) fprintf(stderr, fmt, ##args); })
Packit c6d22b
#define do_log_dump(level, p, len, cr)	({ if (level <= params.verbose) do_dump(stderr, p, len, cr); })
Packit c6d22b
#else
Packit c6d22b
#define do_log(level, fmt, args...)	syslog(level, fmt, ##args)
Packit c6d22b
#define do_log_dump(level, p, len, cr)
Packit c6d22b
#endif
Packit c6d22b
Packit c6d22b
#ifdef DEBUG
Packit c6d22b
#define log_debug(fmt, args...)		do_log(LOG_DEBUG, "%s:%d " fmt, __func__ , __LINE__ , ##args)
Packit c6d22b
#define log_debug_dump(p, len)		do_log_dump(LOG_DEBUG, p, len, true)
Packit c6d22b
#define log_debug_dump_n(p, len)	do_log_dump(LOG_DEBUG, p, len, false)
Packit c6d22b
#else
Packit c6d22b
#define log_debug(fmt, args...)
Packit c6d22b
#define log_debug_dump(p, len)
Packit c6d22b
#endif
Packit c6d22b
Packit c6d22b
#define log_dump(p, len)		do_log_dump(LOG_INFO, p, len, true)
Packit c6d22b
#define log_dump_n(p, len)		do_log_dump(LOG_INFO, p, len, false)
Packit c6d22b
#define log_info(fmt, args...)		do_log(LOG_INFO, fmt, ##args)
Packit c6d22b
#define log_err(fmt, args...)		do_log(LOG_ERR, fmt, ##args)
Packit c6d22b
#define log_errno(fmt, args...)		do_log(LOG_ERR, fmt ": errno: %s (%d)\n", ##args, strerror(errno), errno)
Packit c6d22b
Packit c6d22b
#define	DATA_SIZE	4096
Packit c6d22b
#define SHA1_HASH_LEN   20
Packit c6d22b
Packit c6d22b
#define __packed __attribute__((packed))
Packit c6d22b
Packit c6d22b
enum evm_ima_xattr_type {
Packit c6d22b
	IMA_XATTR_DIGEST = 0x01,
Packit c6d22b
	EVM_XATTR_HMAC,
Packit c6d22b
	EVM_IMA_XATTR_DIGSIG,
Packit c6d22b
	IMA_XATTR_DIGEST_NG,
Packit c6d22b
	EVM_XATTR_PORTABLE_DIGSIG,
Packit c6d22b
};
Packit c6d22b
Packit c6d22b
struct h_misc {
Packit c6d22b
	unsigned long ino;
Packit c6d22b
	uint32_t generation;
Packit c6d22b
	uid_t uid;
Packit c6d22b
	gid_t gid;
Packit c6d22b
	unsigned short mode;
Packit c6d22b
};
Packit c6d22b
Packit c6d22b
struct h_misc_32 {
Packit c6d22b
	uint32_t ino;
Packit c6d22b
	uint32_t generation;
Packit c6d22b
	uid_t uid;
Packit c6d22b
	gid_t gid;
Packit c6d22b
	unsigned short mode;
Packit c6d22b
};
Packit c6d22b
Packit c6d22b
struct h_misc_64 {
Packit c6d22b
	uint64_t ino;
Packit c6d22b
	uint32_t generation;
Packit c6d22b
	uid_t uid;
Packit c6d22b
	gid_t gid;
Packit c6d22b
	unsigned short mode;
Packit c6d22b
};
Packit c6d22b
Packit c6d22b
struct h_misc_digsig {
Packit c6d22b
	uid_t uid;
Packit c6d22b
	gid_t gid;
Packit c6d22b
	unsigned short mode;
Packit c6d22b
};
Packit c6d22b
Packit c6d22b
enum pubkey_algo {
Packit c6d22b
	PUBKEY_ALGO_RSA,
Packit c6d22b
	PUBKEY_ALGO_MAX,
Packit c6d22b
};
Packit c6d22b
Packit c6d22b
enum digest_algo {
Packit c6d22b
	DIGEST_ALGO_SHA1,
Packit c6d22b
	DIGEST_ALGO_SHA256,
Packit c6d22b
	DIGEST_ALGO_MAX
Packit c6d22b
};
Packit c6d22b
Packit c6d22b
enum digsig_version {
Packit c6d22b
	DIGSIG_VERSION_1 = 1,
Packit c6d22b
	DIGSIG_VERSION_2
Packit c6d22b
};
Packit c6d22b
Packit c6d22b
struct pubkey_hdr {
Packit c6d22b
	uint8_t version;	/* key format version */
Packit c6d22b
	uint32_t timestamp;	/* key made, always 0 for now */
Packit c6d22b
	uint8_t algo;
Packit c6d22b
	uint8_t nmpi;
Packit c6d22b
	char mpi[0];
Packit c6d22b
} __packed;
Packit c6d22b
Packit c6d22b
struct signature_hdr {
Packit c6d22b
	uint8_t version;	/* signature format version */
Packit c6d22b
	uint32_t timestamp;	/* signature made */
Packit c6d22b
	uint8_t algo;
Packit c6d22b
	uint8_t hash;
Packit c6d22b
	uint8_t keyid[8];
Packit c6d22b
	uint8_t nmpi;
Packit c6d22b
	char mpi[0];
Packit c6d22b
} __packed;
Packit c6d22b
Packit c6d22b
enum pkey_hash_algo {
Packit c6d22b
	PKEY_HASH_MD4,
Packit c6d22b
	PKEY_HASH_MD5,
Packit c6d22b
	PKEY_HASH_SHA1,
Packit c6d22b
	PKEY_HASH_RIPE_MD_160,
Packit c6d22b
	PKEY_HASH_SHA256,
Packit c6d22b
	PKEY_HASH_SHA384,
Packit c6d22b
	PKEY_HASH_SHA512,
Packit c6d22b
	PKEY_HASH_SHA224,
Packit c6d22b
	PKEY_HASH__LAST
Packit c6d22b
};
Packit c6d22b
Packit c6d22b
/*
Packit c6d22b
 * signature format v2 - for using with asymmetric keys
Packit c6d22b
 */
Packit c6d22b
struct signature_v2_hdr {
Packit c6d22b
	uint8_t version;	/* signature format version */
Packit c6d22b
	uint8_t	hash_algo;	/* Digest algorithm [enum pkey_hash_algo] */
Packit c6d22b
	uint32_t keyid;		/* IMA key identifier - not X509/PGP specific*/
Packit c6d22b
	uint16_t sig_size;	/* signature size */
Packit c6d22b
	uint8_t sig[0];		/* signature payload */
Packit c6d22b
} __packed;
Packit c6d22b
Packit c6d22b
Packit c6d22b
typedef int (*verify_hash_fn_t)(const char *file, const unsigned char *hash, int size, unsigned char *sig, int siglen, const char *keyfile);
Packit c6d22b
Packit c6d22b
struct libevm_params {
Packit c6d22b
	int verbose;
Packit c6d22b
	int x509;
Packit c6d22b
	const char *hash_algo;
Packit c6d22b
	const char *keyfile;
Packit c6d22b
	const char *keypass;
Packit c6d22b
};
Packit c6d22b
Packit c6d22b
struct RSA_ASN1_template {
Packit c6d22b
	const uint8_t *data;
Packit c6d22b
	size_t size;
Packit c6d22b
};
Packit c6d22b
Packit c6d22b
#define	NUM_PCRS 20
Packit c6d22b
#define DEFAULT_PCR 10
Packit c6d22b
Packit c6d22b
extern const struct RSA_ASN1_template RSA_ASN1_templates[PKEY_HASH__LAST];
Packit c6d22b
extern struct libevm_params params;
Packit c6d22b
Packit c6d22b
void do_dump(FILE *fp, const void *ptr, int len, bool cr);
Packit c6d22b
void dump(const void *ptr, int len);
Packit c6d22b
int get_filesize(const char *filename);
Packit c6d22b
int ima_calc_hash(const char *file, uint8_t *hash);
Packit c6d22b
int get_hash_algo(const char *algo);
Packit c6d22b
RSA *read_pub_key(const char *keyfile, int x509);
Packit c6d22b
Packit c6d22b
void calc_keyid_v1(uint8_t *keyid, char *str, const unsigned char *pkey, int len);
Packit c6d22b
void calc_keyid_v2(uint32_t *keyid, char *str, RSA *key);
Packit c6d22b
int key2bin(RSA *key, unsigned char *pub);
Packit c6d22b
Packit c6d22b
int sign_hash(const char *algo, const unsigned char *hash, int size, const char *keyfile, const char *keypass, unsigned char *sig);
Packit c6d22b
int verify_hash(const char *file, const unsigned char *hash, int size, unsigned char *sig, int siglen);
Packit c6d22b
int ima_verify_signature(const char *file, unsigned char *sig, int siglen, unsigned char *digest, int digestlen);
Packit c6d22b
void init_public_keys(const char *keyfiles);
Packit c6d22b
Packit c6d22b
#endif