Changed to conform Linux kernel coding style
Changed to conform Linux kernel coding style, except 80 characters line length limit. Signed-off-by: Dmitry Kasatkin <dmitry.kasatkin@intel.com>
This commit is contained in:
		
							
								
								
									
										62
									
								
								src/evmctl.c
									
									
									
									
									
								
							
							
						
						
									
										62
									
								
								src/evmctl.c
									
									
									
									
									
								
							| @@ -56,8 +56,8 @@ | |||||||
| #define USE_FPRINTF | #define USE_FPRINTF | ||||||
|  |  | ||||||
| #ifdef USE_FPRINTF | #ifdef USE_FPRINTF | ||||||
| #define do_log(level, fmt, args...)	if (level <= verbose) fprintf(stderr, fmt, ##args) | #define do_log(level, fmt, args...)	({ if (level <= verbose) fprintf(stderr, fmt, ##args); }) | ||||||
| #define do_log_dump(level, p, len)	if (level <= verbose) do_dump(stderr, p, len) | #define do_log_dump(level, p, len)	({ if (level <= verbose) do_dump(stderr, p, len); }) | ||||||
| #else | #else | ||||||
| #define do_log(level, fmt, args...)	syslog(level, fmt, ##args) | #define do_log(level, fmt, args...)	syslog(level, fmt, ##args) | ||||||
| #define do_log_dump(p, len) | #define do_log_dump(p, len) | ||||||
| @@ -114,7 +114,6 @@ struct pubkey_hdr { | |||||||
| 	char mpi[0]; | 	char mpi[0]; | ||||||
| } __attribute__ ((packed)); | } __attribute__ ((packed)); | ||||||
|  |  | ||||||
|  |  | ||||||
| struct signature_hdr { | struct signature_hdr { | ||||||
| 	uint8_t version;	/* signature format version */ | 	uint8_t version;	/* signature format version */ | ||||||
| 	time_t timestamp;	/* signature made */ | 	time_t timestamp;	/* signature made */ | ||||||
| @@ -125,7 +124,6 @@ struct signature_hdr { | |||||||
| 	char mpi[0]; | 	char mpi[0]; | ||||||
| } __attribute__ ((packed)); | } __attribute__ ((packed)); | ||||||
|  |  | ||||||
|  |  | ||||||
| static char *evm_config_xattrnames[] = { | static char *evm_config_xattrnames[] = { | ||||||
| 	"security.selinux", | 	"security.selinux", | ||||||
| 	"security.SMACK64", | 	"security.SMACK64", | ||||||
| @@ -146,23 +144,22 @@ static int		verbose = LOG_INFO - 1; | |||||||
| static int g_argc; | static int g_argc; | ||||||
| static char **g_argv; | static char **g_argv; | ||||||
| static int set_xattr = 1; | static int set_xattr = 1; | ||||||
| static int		digest = 0; | static int digest; | ||||||
| static int		digsig = 0; | static int digsig; | ||||||
| static char *hash_algo = "sha1"; | static char *hash_algo = "sha1"; | ||||||
| static int		binkey = 0; | static int binkey; | ||||||
| static char *keypass; | static char *keypass; | ||||||
|  |  | ||||||
| extern struct command	cmds[]; | struct command cmds[]; | ||||||
| static void print_usage(struct command *cmd); | static void print_usage(struct command *cmd); | ||||||
|  |  | ||||||
| static void do_dump(FILE *fp, const void *ptr, int len) | static void do_dump(FILE *fp, const void *ptr, int len) | ||||||
| { | { | ||||||
| 	int i; | 	int i; | ||||||
|         uint8_t *data = (uint8_t *)ptr; | 	uint8_t *data = (uint8_t *) ptr; | ||||||
|  |  | ||||||
|         for (i = 0; i < len; i++) { | 	for (i = 0; i < len; i++) | ||||||
| 		fprintf(fp, "%02x", data[i]); | 		fprintf(fp, "%02x", data[i]); | ||||||
|         } |  | ||||||
| 	fprintf(fp, "\n"); | 	fprintf(fp, "\n"); | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -176,7 +173,7 @@ static inline int get_filesize(const char *filename) | |||||||
| 	struct stat stats; | 	struct stat stats; | ||||||
| 	/*  Need to know the file length */ | 	/*  Need to know the file length */ | ||||||
| 	stat(filename, &stats); | 	stat(filename, &stats); | ||||||
| 	return (int) stats.st_size; | 	return (int)stats.st_size; | ||||||
| } | } | ||||||
|  |  | ||||||
| static inline int get_fdsize(int fd) | static inline int get_fdsize(int fd) | ||||||
| @@ -184,7 +181,7 @@ static inline int get_fdsize(int fd) | |||||||
| 	struct stat stats; | 	struct stat stats; | ||||||
| 	/*  Need to know the file length */ | 	/*  Need to know the file length */ | ||||||
| 	fstat(fd, &stats); | 	fstat(fd, &stats); | ||||||
| 	return (int) stats.st_size; | 	return (int)stats.st_size; | ||||||
| } | } | ||||||
|  |  | ||||||
| static int bin2file(const char *file, const char *ext, const unsigned char *data, int len) | static int bin2file(const char *file, const char *ext, const unsigned char *data, int len) | ||||||
| @@ -247,7 +244,6 @@ static int key2bin(RSA *key, unsigned char *pub) | |||||||
|  |  | ||||||
| 	offset += sizeof(*pkh); | 	offset += sizeof(*pkh); | ||||||
|  |  | ||||||
| 	// MPIs |  | ||||||
| 	len = BN_num_bytes(key->n); | 	len = BN_num_bytes(key->n); | ||||||
| 	b = BN_num_bits(key->n); | 	b = BN_num_bits(key->n); | ||||||
| 	pub[offset++] = b >> 8; | 	pub[offset++] = b >> 8; | ||||||
| @@ -291,7 +287,6 @@ static int read_key(const char *inkey, unsigned char *pub) | |||||||
| 	return len; | 	return len; | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| static void calc_keyid(uint8_t *keyid, char *str, const unsigned char *pkey, int len) | static void calc_keyid(uint8_t *keyid, char *str, const unsigned char *pkey, int len) | ||||||
| { | { | ||||||
| 	uint8_t sha1[SHA_DIGEST_LENGTH]; | 	uint8_t sha1[SHA_DIGEST_LENGTH]; | ||||||
| @@ -301,12 +296,12 @@ static void calc_keyid(uint8_t *keyid, char *str, const unsigned char *pkey, int | |||||||
| 	log_debug_dump(pkey, len); | 	log_debug_dump(pkey, len); | ||||||
| 	SHA1(pkey, len, sha1); | 	SHA1(pkey, len, sha1); | ||||||
|  |  | ||||||
| 	//sha1[12 - 19] is exactly keyid from gpg file | 	/* sha1[12 - 19] is exactly keyid from gpg file */ | ||||||
| 	memcpy(keyid, sha1 + 12, 8); | 	memcpy(keyid, sha1 + 12, 8); | ||||||
| 	log_debug("keyid:\n"); | 	log_debug("keyid:\n"); | ||||||
| 	log_debug_dump(keyid, 8); | 	log_debug_dump(keyid, 8); | ||||||
|  |  | ||||||
| 	id = __be64_to_cpup((__be64 *)keyid); | 	id = __be64_to_cpup((__be64 *) keyid); | ||||||
| 	sprintf(str, "%llX", (unsigned long long)id); | 	sprintf(str, "%llX", (unsigned long long)id); | ||||||
| 	log_info("keyid: %s\n", str); | 	log_info("keyid: %s\n", str); | ||||||
| } | } | ||||||
| @@ -366,7 +361,7 @@ static int sign_hash(const unsigned char *hash, int size, const char *keyfile, u | |||||||
| 	len = err; | 	len = err; | ||||||
|  |  | ||||||
| 	/* we add bit length of the signature to make it gnupg compatible */ | 	/* we add bit length of the signature to make it gnupg compatible */ | ||||||
| 	blen = (uint16_t *)(sig + sizeof(*hdr)); | 	blen = (uint16_t *) (sig + sizeof(*hdr)); | ||||||
| 	*blen = __cpu_to_be16(len << 3); | 	*blen = __cpu_to_be16(len << 3); | ||||||
| 	len += sizeof(*hdr) + 2; | 	len += sizeof(*hdr) + 2; | ||||||
| 	log_info("evm/ima signature: %d bytes\n", len); | 	log_info("evm/ima signature: %d bytes\n", len); | ||||||
| @@ -425,7 +420,7 @@ static int calc_evm_hash(const char *file, const char *keyfile, unsigned char *h | |||||||
| 			log_info("no attr: %s\n", *xattrname); | 			log_info("no attr: %s\n", *xattrname); | ||||||
| 			continue; | 			continue; | ||||||
| 		} | 		} | ||||||
| 		//log_debug("name: %s, value: %s, size: %d\n", *xattrname, xattr_value, err); | 		/*log_debug("name: %s, value: %s, size: %d\n", *xattrname, xattr_value, err);*/ | ||||||
| 		log_info("name: %s, size: %d\n", *xattrname, err); | 		log_info("name: %s, size: %d\n", *xattrname, err); | ||||||
| 		log_debug_dump(xattr_value, err); | 		log_debug_dump(xattr_value, err); | ||||||
| 		err = EVP_DigestUpdate(&ctx, xattr_value, err); | 		err = EVP_DigestUpdate(&ctx, xattr_value, err); | ||||||
| @@ -442,7 +437,7 @@ static int calc_evm_hash(const char *file, const char *keyfile, unsigned char *h | |||||||
| 	hmac_misc.gid = st.st_gid; | 	hmac_misc.gid = st.st_gid; | ||||||
| 	hmac_misc.mode = st.st_mode; | 	hmac_misc.mode = st.st_mode; | ||||||
|  |  | ||||||
| 	err = EVP_DigestUpdate(&ctx, (const unsigned char*)&hmac_misc, sizeof(hmac_misc)); | 	err = EVP_DigestUpdate(&ctx, (const unsigned char *)&hmac_misc, sizeof(hmac_misc)); | ||||||
| 	if (!err) { | 	if (!err) { | ||||||
| 		log_errno("EVP_DigestUpdate() failed"); | 		log_errno("EVP_DigestUpdate() failed"); | ||||||
| 		return -1; | 		return -1; | ||||||
| @@ -578,7 +573,7 @@ static int calc_dir_hash(const char *file, uint8_t *hash) | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	while ((de = readdir(dir))) { | 	while ((de = readdir(dir))) { | ||||||
| 		//printf("entry: ino: %lu, %s\n", de->d_ino, de->d_name); | 		/*log_debug("entry: ino: %lu, %s\n", de->d_ino, de->d_name);*/ | ||||||
| 		for (prev = NULL, pos = head; pos; prev = pos, pos = pos->next) { | 		for (prev = NULL, pos = head; pos; prev = pos, pos = pos->next) { | ||||||
| 			if (de->d_ino < pos->de.d_ino) | 			if (de->d_ino < pos->de.d_ino) | ||||||
| 				break; | 				break; | ||||||
| @@ -622,7 +617,7 @@ static int calc_dir_hash(const char *file, uint8_t *hash) | |||||||
|  |  | ||||||
| static int hash_ima(const char *file) | static int hash_ima(const char *file) | ||||||
| { | { | ||||||
| 	unsigned char hash[65] = "\x01";// MAX hash size + 1 | 	unsigned char hash[65] = "\x01"; /* MAX hash size + 1 */ | ||||||
| 	int err; | 	int err; | ||||||
| 	struct stat st; | 	struct stat st; | ||||||
|  |  | ||||||
| @@ -788,7 +783,7 @@ static int verify_hash(const unsigned char *hash, int size, unsigned char *sig, | |||||||
| 		log_errno("Verification failed: %d", err); | 		log_errno("Verification failed: %d", err); | ||||||
| 		return -1; | 		return -1; | ||||||
| 	} else { | 	} else { | ||||||
| 		//log_info("Verification is OK\n"); | 		/*log_info("Verification is OK\n");*/ | ||||||
| 		printf("Verification is OK\n"); | 		printf("Verification is OK\n"); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @@ -1005,7 +1000,7 @@ static int calc_evm_hmac(const char *file, const char *keyfile, unsigned char *h | |||||||
| 			log_info("no attr: %s\n", *xattrname); | 			log_info("no attr: %s\n", *xattrname); | ||||||
| 			continue; | 			continue; | ||||||
| 		} | 		} | ||||||
| 		//log_debug("name: %s, value: %s, size: %d\n", *xattrname, xattr_value, err); | 		/*log_debug("name: %s, value: %s, size: %d\n", *xattrname, xattr_value, err);*/ | ||||||
| 		log_info("name: %s, size: %d\n", *xattrname, err); | 		log_info("name: %s, size: %d\n", *xattrname, err); | ||||||
| 		log_debug_dump(xattr_value, err); | 		log_debug_dump(xattr_value, err); | ||||||
| 		HMAC_Update(&ctx, xattr_value, err); | 		HMAC_Update(&ctx, xattr_value, err); | ||||||
| @@ -1018,7 +1013,7 @@ static int calc_evm_hmac(const char *file, const char *keyfile, unsigned char *h | |||||||
| 	hmac_misc.gid = st.st_gid; | 	hmac_misc.gid = st.st_gid; | ||||||
| 	hmac_misc.mode = st.st_mode; | 	hmac_misc.mode = st.st_mode; | ||||||
|  |  | ||||||
| 	HMAC_Update(&ctx, (const unsigned char*)&hmac_misc, sizeof(hmac_misc)); | 	HMAC_Update(&ctx, (const unsigned char *)&hmac_misc, sizeof(hmac_misc)); | ||||||
| 	HMAC_Final(&ctx, hash, &mdlen); | 	HMAC_Final(&ctx, hash, &mdlen); | ||||||
| 	HMAC_CTX_cleanup(&ctx); | 	HMAC_CTX_cleanup(&ctx); | ||||||
|  |  | ||||||
| @@ -1151,13 +1146,13 @@ static void usage(void) | |||||||
|  |  | ||||||
| struct command cmds[] = { | struct command cmds[] = { | ||||||
| 	{"help", cmd_help, 0, "<command>"}, | 	{"help", cmd_help, 0, "<command>"}, | ||||||
| 	{"import", cmd_import, 0, "[--bin] inkey keyring", "Import public key (PEM/bin) into the keyring.\n" }, | 	{"import", cmd_import, 0, "[--bin] inkey keyring", "Import public key (PEM/bin) into the keyring.\n"}, | ||||||
| 	{"convert", cmd_convert, 0, "inkey outkey", "Convert PEM public key into IMA/EVM kernel friendly format.\n" }, | 	{"convert", cmd_convert, 0, "inkey outkey", "Convert PEM public key into IMA/EVM kernel friendly format.\n"}, | ||||||
| 	{"sign", cmd_sign_evm, 0, "[--imahash | --imasig ] file [key]", "Sign file metadata.\n" }, | 	{"sign", cmd_sign_evm, 0, "[--imahash | --imasig ] file [key]", "Sign file metadata.\n"}, | ||||||
| 	{"verify", cmd_verify_evm, 0, "file", "Verify EVM signature (for debugging).\n" }, | 	{"verify", cmd_verify_evm, 0, "file", "Verify EVM signature (for debugging).\n"}, | ||||||
| 	{"ima_sign", cmd_sign_ima, 0, "file [key]", "Sign file content.\n" }, | 	{"ima_sign", cmd_sign_ima, 0, "file [key]", "Sign file content.\n"}, | ||||||
| 	{"ima_hash", cmd_hash_ima, 0, "file", "Hash file content.\n" }, | 	{"ima_hash", cmd_hash_ima, 0, "file", "Hash file content.\n"}, | ||||||
| 	{"hmac", cmd_hmac_evm, 0, "[--imahash | --imasig ] file [key]", "Sign file metadata with HMAC (for debugging).\n" }, | 	{"hmac", cmd_hmac_evm, 0, "[--imahash | --imasig ] file [key]", "Sign file metadata with HMAC (for debugging).\n"}, | ||||||
| 	{0, 0, 0, NULL} | 	{0, 0, 0, NULL} | ||||||
| }; | }; | ||||||
|  |  | ||||||
| @@ -1203,7 +1198,8 @@ int main(int argc, char *argv[]) | |||||||
| 			digsig = 1; | 			digsig = 1; | ||||||
| 			break; | 			break; | ||||||
| 		case 'n': | 		case 'n': | ||||||
| 			set_xattr = 0; // do not set Extended Attributes... just print signature | 			/* do not set Extended Attributes... just print signature */ | ||||||
|  | 			set_xattr = 0; | ||||||
| 			break; | 			break; | ||||||
| 		case 'a': | 		case 'a': | ||||||
| 			hash_algo = optarg; | 			hash_algo = optarg; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Dmitry Kasatkin
					Dmitry Kasatkin