Blame SOURCES/0008-use-best-cipher.patch

115784
From 260293c0ee5a268bc7fc1483e5304546745122a6 Mon Sep 17 00:00:00 2001
115784
From: Vernon Mauery <vernon.mauery@intel.com>
115784
Date: Mon, 9 Apr 2018 12:28:57 -0700
115784
Subject: [PATCH] lanplus: Auto-select 'best' cipher suite available
115784
115784
Current cipher suites could be ranked as this:
115784
  17 > 3 >> all the rest
115784
115784
Cherry-picked-from: 7772254b62826b894ca629df8c597030a98f4f72
115784
Cherry-picked-from: f9c699c712f884c82fc1a62f1f61a8d597ac0cfd
115784
115784
Also fetched some functions/macros for helper.h
115784
115784
Equals to getting include/ipmitool/helper.h changes from commits:
115784
(oldest first)
115784
115784
  6c00d44 mc: watchdog get: Update to match IPMI 2.0 spec
115784
  e8e94d8 mc: watchdog set: Refactor to reduce complexity
115784
  0310208 mc: Code refactor to reduce copy-paste ratio
115784
  249e092 general: Make byteswapping arch-independent
115784
  5491b12 refix 249e0929: Fix byteswapping helpers
115784
  bb1a4cc Refactoring. Improve code reuse ratio.
115784
---
115784
 include/ipmitool/helper.h       |  58 ++++++++
115784
 include/ipmitool/ipmi_channel.h |  47 +++++++
115784
 include/ipmitool/ipmi_intf.h    |  39 ++++-
115784
 lib/ipmi_channel.c              | 242 +++++++++++++++-----------------
115784
 lib/ipmi_main.c                 |  23 +--
115784
 src/plugins/ipmi_intf.c         |   5 +-
115784
 src/plugins/lanplus/lanplus.c   | 114 +++++++++++----
115784
 7 files changed, 360 insertions(+), 168 deletions(-)
115784
115784
diff --git a/include/ipmitool/helper.h b/include/ipmitool/helper.h
115784
index c53736f..6a2e5f4 100644
115784
--- a/include/ipmitool/helper.h
115784
+++ b/include/ipmitool/helper.h
115784
@@ -38,6 +38,8 @@
115784
 #include <stdio.h>
115784
 #include <string.h>
115784
 
115784
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
115784
+
115784
 #ifndef TRUE
115784
 #define TRUE    1
115784
 #endif
115784
@@ -111,6 +113,62 @@ FILE * ipmi_open_file(const char * file, int rw);
115784
 void ipmi_start_daemon(struct ipmi_intf *intf);
115784
 uint16_t ipmi_get_oem_id(struct ipmi_intf *intf);
115784
 
115784
+#define IS_SET(v, b) ((v) & (1 << (b)))
115784
+
115784
+/* le16toh(), hto16le(), et. al. don't exist for Windows or Apple */
115784
+/* For portability, let's simply define our own versions here */
115784
+
115784
+/* IPMI is always little-endian */
115784
+static inline uint16_t ipmi16toh(void *ipmi16)
115784
+{
115784
+	uint8_t *ipmi = (uint8_t *)ipmi16;
115784
+	uint16_t h;
115784
+
115784
+	h = ipmi[1] << 8; /* MSB */
115784
+	h |= ipmi[0]; /* LSB */
115784
+
115784
+	return h;
115784
+}
115784
+
115784
+static inline void htoipmi16(uint16_t h, uint8_t *ipmi)
115784
+{
115784
+	ipmi[0] = h & 0xFF; /* LSB */
115784
+	ipmi[1] = h >> 8; /* MSB */
115784
+}
115784
+
115784
+static inline uint32_t ipmi24toh(void *ipmi24)
115784
+{
115784
+	uint8_t *ipmi = (uint8_t *)ipmi24;
115784
+	uint32_t h = 0;
115784
+
115784
+	h = ipmi[2] << 16; /* MSB */
115784
+	h |= ipmi[1] << 8;
115784
+	h |= ipmi[0]; /* LSB */
115784
+
115784
+	return h;
115784
+}
115784
+
115784
+static inline uint32_t ipmi32toh(void *ipmi32)
115784
+{
115784
+	uint8_t *ipmi = ipmi32;
115784
+	uint32_t h;
115784
+
115784
+	h = ipmi[3] << 24; /* MSB */
115784
+	h |= ipmi[2] << 16;
115784
+	h |= ipmi[1] << 8;
115784
+	h |= ipmi[0]; /* LSB */
115784
+
115784
+	return h;
115784
+}
115784
+
115784
+static inline void htoipmi32(uint32_t h, uint8_t *ipmi)
115784
+{
115784
+	ipmi[0] = h & 0xFF; /* LSB */
115784
+	ipmi[1] = (h >> 8) & 0xFF;
115784
+	ipmi[2] = (h >> 16) & 0xFF;
115784
+	ipmi[3] = (h >> 24) & 0xFF; /* MSB */
115784
+}
115784
+
115784
 #define ipmi_open_file_read(file)	ipmi_open_file(file, 0)
115784
 #define ipmi_open_file_write(file)	ipmi_open_file(file, 1)
115784
 
115784
diff --git a/include/ipmitool/ipmi_channel.h b/include/ipmitool/ipmi_channel.h
115784
index 3ade2d5..324c0bb 100644
115784
--- a/include/ipmitool/ipmi_channel.h
115784
+++ b/include/ipmitool/ipmi_channel.h
115784
@@ -37,6 +37,7 @@
115784
 # include <config.h>
115784
 #endif
115784
 #include <ipmitool/ipmi.h>
115784
+#include <ipmitool/ipmi_intf.h>
115784
 
115784
 
115784
 #define IPMI_GET_CHANNEL_AUTH_CAP      0x38
115784
@@ -81,6 +82,50 @@ struct channel_access_t {
115784
 	uint8_t user_level_auth;
115784
 };
115784
 
115784
+/*
115784
+ * The Cipher Suite Record Format from table 22-18 of the IPMI v2.0 spec
115784
+ */
115784
+enum cipher_suite_format_tag {
115784
+	STANDARD_CIPHER_SUITE = 0xc0,
115784
+	OEM_CIPHER_SUITE = 0xc1,
115784
+};
115784
+#ifdef HAVE_PRAGMA_PACK
115784
+#pragma pack(1)
115784
+#endif
115784
+struct std_cipher_suite_record_t {
115784
+	uint8_t start_of_record;
115784
+	uint8_t cipher_suite_id;
115784
+	uint8_t auth_alg;
115784
+	uint8_t integrity_alg;
115784
+	uint8_t crypt_alg;
115784
+} ATTRIBUTE_PACKING;
115784
+struct oem_cipher_suite_record_t {
115784
+	uint8_t start_of_record;
115784
+	uint8_t cipher_suite_id;
115784
+	uint8_t iana[3];
115784
+	uint8_t auth_alg;
115784
+	uint8_t integrity_alg;
115784
+	uint8_t crypt_alg;
115784
+} ATTRIBUTE_PACKING;
115784
+#ifdef HAVE_PRAGMA_PACK
115784
+#pragma pack(0)
115784
+#endif
115784
+#define CIPHER_ALG_MASK 0x3f
115784
+#define MAX_CIPHER_SUITE_RECORD_OFFSET 0x40
115784
+#define MAX_CIPHER_SUITE_DATA_LEN 0x10
115784
+#define LIST_ALGORITHMS_BY_CIPHER_SUITE 0x80
115784
+
115784
+/* Below is the theoretical maximum number of cipher suites that could be
115784
+ * reported by a BMC. That is with the Get Channel Cipher Suites Command, at 16
115784
+ * bytes at a time and 0x40 requests, it can report 1024 bytes, which is about
115784
+ * 204 standard records or 128 OEM records. Really, we probably don't need more
115784
+ * than about 20, which is the full set of standard records plus a few OEM
115784
+ * records.
115784
+ */
115784
+#define MAX_CIPHER_SUITE_COUNT (MAX_CIPHER_SUITE_RECORD_OFFSET * \
115784
+		MAX_CIPHER_SUITE_DATA_LEN / \
115784
+		sizeof(struct std_cipher_suite_record_t))
115784
+
115784
 /*
115784
  * The Get Authentication Capabilities response structure
115784
  * From table 22-15 of the IPMI v2.0 spec
115784
@@ -135,6 +180,8 @@ struct get_channel_auth_cap_rsp {
115784
 int _ipmi_get_channel_access(struct ipmi_intf *intf,
115784
 		struct channel_access_t *channel_access,
115784
 		uint8_t get_volatile_settings);
115784
+int ipmi_get_channel_cipher_suites(struct ipmi_intf *intf, const char *payload_type,
115784
+		uint8_t channel, struct cipher_suite_info *suites, size_t *count);
115784
 int _ipmi_get_channel_info(struct ipmi_intf *intf,
115784
         struct channel_info_t *channel_info);
115784
 int _ipmi_set_channel_access(struct ipmi_intf *intf,
115784
diff --git a/include/ipmitool/ipmi_intf.h b/include/ipmitool/ipmi_intf.h
115784
index 0b8c64b..7a07d66 100644
115784
--- a/include/ipmitool/ipmi_intf.h
115784
+++ b/include/ipmitool/ipmi_intf.h
115784
@@ -61,13 +61,45 @@ enum LANPLUS_SESSION_STATE {
115784
 #define IPMI_AUTHCODE_BUFFER_SIZE 20
115784
 #define IPMI_SIK_BUFFER_SIZE      IPMI_MAX_MD_SIZE
115784
 
115784
+enum cipher_suite_ids {
115784
+	IPMI_LANPLUS_CIPHER_SUITE_0 = 0,
115784
+	IPMI_LANPLUS_CIPHER_SUITE_1 = 1,
115784
+	IPMI_LANPLUS_CIPHER_SUITE_2 = 2,
115784
+	IPMI_LANPLUS_CIPHER_SUITE_3 = 3,
115784
+	IPMI_LANPLUS_CIPHER_SUITE_4 = 4,
115784
+	IPMI_LANPLUS_CIPHER_SUITE_5 = 5,
115784
+	IPMI_LANPLUS_CIPHER_SUITE_6 = 6,
115784
+	IPMI_LANPLUS_CIPHER_SUITE_7 = 7,
115784
+	IPMI_LANPLUS_CIPHER_SUITE_8 = 8,
115784
+	IPMI_LANPLUS_CIPHER_SUITE_9 = 9,
115784
+	IPMI_LANPLUS_CIPHER_SUITE_10 = 10,
115784
+	IPMI_LANPLUS_CIPHER_SUITE_11 = 11,
115784
+	IPMI_LANPLUS_CIPHER_SUITE_12 = 12,
115784
+	IPMI_LANPLUS_CIPHER_SUITE_13 = 13,
115784
+	IPMI_LANPLUS_CIPHER_SUITE_14 = 14,
115784
+#ifdef HAVE_CRYPTO_SHA256
115784
+	IPMI_LANPLUS_CIPHER_SUITE_15 = 15,
115784
+	IPMI_LANPLUS_CIPHER_SUITE_16 = 16,
115784
+	IPMI_LANPLUS_CIPHER_SUITE_17 = 17,
115784
+#endif /* HAVE_CRYPTO_SHA256 */
115784
+	IPMI_LANPLUS_CIPHER_SUITE_RESERVED = 0xff,
115784
+};
115784
+
115784
+struct cipher_suite_info {
115784
+	enum cipher_suite_ids cipher_suite_id;
115784
+	uint8_t auth_alg;
115784
+	uint8_t integrity_alg;
115784
+	uint8_t crypt_alg;
115784
+	uint32_t iana;
115784
+};
115784
+
115784
 struct ipmi_session_params {
115784
 	char * hostname;
115784
 	uint8_t username[17];
115784
 	uint8_t authcode_set[IPMI_AUTHCODE_BUFFER_SIZE + 1];
115784
 	uint8_t authtype_set;
115784
 	uint8_t privlvl;
115784
-	uint8_t cipher_suite_id;
115784
+	enum cipher_suite_ids cipher_suite_id;
115784
 	char sol_escape_char;
115784
 	int password;
115784
 	int port;
115784
@@ -217,7 +249,10 @@ void ipmi_intf_session_set_username(struct ipmi_intf * intf, char * username);
115784
 void ipmi_intf_session_set_password(struct ipmi_intf * intf, char * password);
115784
 void ipmi_intf_session_set_privlvl(struct ipmi_intf * intf, uint8_t privlvl);
115784
 void ipmi_intf_session_set_lookupbit(struct ipmi_intf * intf, uint8_t lookupbit);
115784
-void ipmi_intf_session_set_cipher_suite_id(struct ipmi_intf * intf, uint8_t cipher_suite_id);
115784
+#ifdef IPMI_INTF_LANPLUS
115784
+void ipmi_intf_session_set_cipher_suite_id(struct ipmi_intf * intf,
115784
+		enum cipher_suite_ids cipher_suite_id);
115784
+#endif /* IPMI_INTF_LANPLUS */
115784
 void ipmi_intf_session_set_sol_escape_char(struct ipmi_intf * intf, char sol_escape_char);
115784
 void ipmi_intf_session_set_kgkey(struct ipmi_intf *intf, const uint8_t *kgkey);
115784
 void ipmi_intf_session_set_port(struct ipmi_intf * intf, int port);
115784
diff --git a/lib/ipmi_channel.c b/lib/ipmi_channel.c
115784
index e1fc75f..3ae3104 100644
115784
--- a/lib/ipmi_channel.c
115784
+++ b/lib/ipmi_channel.c
115784
@@ -342,86 +342,116 @@ ipmi_get_channel_auth_cap(struct ipmi_intf *intf, uint8_t channel, uint8_t priv)
115784
 	return 0;
115784
 }
115784
 
115784
-static int
115784
+static size_t
115784
+parse_channel_cipher_suite_data(uint8_t *cipher_suite_data, size_t data_len,
115784
+		struct cipher_suite_info* suites, size_t nr_suites)
115784
+{
115784
+	size_t count = 0;
115784
+	size_t offset = 0;
115784
+	uint32_t iana;
115784
+	uint8_t auth_alg, integrity_alg, crypt_alg;
115784
+	uint8_t cipher_suite_id;
115784
+
115784
+	memset(suites, 0, sizeof(*suites) * nr_suites);
115784
+
115784
+	while (offset < data_len && count < nr_suites) {
115784
+		auth_alg      = IPMI_AUTH_RAKP_NONE;
115784
+		integrity_alg = IPMI_INTEGRITY_NONE;
115784
+		crypt_alg     = IPMI_CRYPT_NONE;
115784
+		if (cipher_suite_data[offset] == STANDARD_CIPHER_SUITE) {
115784
+			struct std_cipher_suite_record_t *record =
115784
+				(struct std_cipher_suite_record_t*)(&cipher_suite_data[offset]);
115784
+			/* standard type */
115784
+			iana = 0;
115784
+
115784
+			/* Verify that we have at least a full record left; id + 3 algs */
115784
+			if ((data_len - offset) < sizeof(*record)) {
115784
+				lprintf(LOG_INFO, "Incomplete data record in cipher suite data");
115784
+				break;
115784
+			}
115784
+			cipher_suite_id = record->cipher_suite_id;
115784
+			auth_alg = CIPHER_ALG_MASK & record->auth_alg;
115784
+			integrity_alg = CIPHER_ALG_MASK & record->integrity_alg;
115784
+			crypt_alg = CIPHER_ALG_MASK & record->crypt_alg;
115784
+			offset += sizeof(*record);
115784
+		} else if (cipher_suite_data[offset] == OEM_CIPHER_SUITE) {
115784
+			/* OEM record type */
115784
+			struct oem_cipher_suite_record_t *record =
115784
+				(struct oem_cipher_suite_record_t*)(&cipher_suite_data[offset]);
115784
+			/* Verify that we have at least a full record left
115784
+			 * id + iana + 3 algs
115784
+			 */
115784
+			if ((data_len - offset) < sizeof(*record)) {
115784
+				lprintf(LOG_INFO, "Incomplete data record in cipher suite data");
115784
+				break;
115784
+			}
115784
+
115784
+			cipher_suite_id = record->cipher_suite_id;
115784
+
115784
+			/* Grab the IANA */
115784
+			iana = ipmi24toh(record->iana);
115784
+			auth_alg = CIPHER_ALG_MASK & record->auth_alg;
115784
+			integrity_alg = CIPHER_ALG_MASK & record->integrity_alg;
115784
+			crypt_alg = CIPHER_ALG_MASK & record->crypt_alg;
115784
+			offset += sizeof(*record);
115784
+		} else {
115784
+			lprintf(LOG_INFO, "Bad start of record byte in cipher suite data (offset %d, value %x)", offset, cipher_suite_data[offset]);
115784
+			break;
115784
+		}
115784
+		suites[count].cipher_suite_id = cipher_suite_id;
115784
+		suites[count].iana = iana;
115784
+		suites[count].auth_alg = auth_alg;
115784
+		suites[count].integrity_alg = integrity_alg;
115784
+		suites[count].crypt_alg = crypt_alg;
115784
+		count++;
115784
+	}
115784
+	return count;
115784
+}
115784
+
115784
+int
115784
 ipmi_get_channel_cipher_suites(struct ipmi_intf *intf, const char *payload_type,
115784
-		uint8_t channel)
115784
+		uint8_t channel, struct cipher_suite_info *suites, size_t *count)
115784
 {
115784
 	struct ipmi_rs *rsp;
115784
 	struct ipmi_rq req;
115784
 
115784
 	uint8_t rqdata[3];
115784
-	uint32_t iana;
115784
-	uint8_t auth_alg, integrity_alg, crypt_alg;
115784
-	uint8_t cipher_suite_id;
115784
 	uint8_t list_index = 0;
115784
 	/* 0x40 sets * 16 bytes per set */
115784
-	uint8_t cipher_suite_data[1024];
115784
-	uint16_t offset = 0;
115784
-	/* how much was returned, total */
115784
-	uint16_t cipher_suite_data_length = 0;
115784
+	uint8_t cipher_suite_data[MAX_CIPHER_SUITE_RECORD_OFFSET *
115784
+		MAX_CIPHER_SUITE_DATA_LEN];
115784
+	size_t offset = 0;
115784
+	size_t nr_suites = 0;
115784
 
115784
+	if (!suites || !count || !*count)
115784
+		return -1;
115784
+
115784
+	nr_suites = *count;
115784
+	*count = 0;
115784
 	memset(cipher_suite_data, 0, sizeof(cipher_suite_data));
115784
-	
115784
+
115784
 	memset(&req, 0, sizeof(req));
115784
 	req.msg.netfn = IPMI_NETFN_APP;
115784
 	req.msg.cmd = IPMI_GET_CHANNEL_CIPHER_SUITES;
115784
 	req.msg.data = rqdata;
115784
-	req.msg.data_len = 3;
115784
+	req.msg.data_len = sizeof(rqdata);
115784
 
115784
 	rqdata[0] = channel;
115784
 	rqdata[1] = ((strncmp(payload_type, "ipmi", 4) == 0)? 0: 1);
115784
-	/* Always ask for cipher suite format */
115784
-	rqdata[2] = 0x80;
115784
-
115784
-	rsp = intf->sendrecv(intf, &req;;
115784
-	if (rsp == NULL) {
115784
-		lprintf(LOG_ERR, "Unable to Get Channel Cipher Suites");
115784
-		return -1;
115784
-	}
115784
-	if (rsp->ccode > 0) {
115784
-		lprintf(LOG_ERR, "Get Channel Cipher Suites failed: %s",
115784
-			val2str(rsp->ccode, completion_code_vals));
115784
-		return -1;
115784
-	}
115784
-
115784
-
115784
-	/*
115784
-	 * Grab the returned channel number once.  We assume it's the same
115784
-	 * in future calls.
115784
-	 */
115784
-	if (rsp->data_len >= 1) {
115784
-		channel = rsp->data[0];
115784
-	}
115784
-
115784
-	while ((rsp->data_len > 1) && (rsp->data_len == 17) && (list_index < 0x3F)) {
115784
-		/*
115784
-		 * We got back cipher suite data -- store it.
115784
-		 * printf("copying data to offset %d\n", offset);
115784
-		 * printbuf(rsp->data + 1, rsp->data_len - 1, "this is the data");
115784
-		 */
115784
-		memcpy(cipher_suite_data + offset, rsp->data + 1, rsp->data_len - 1);
115784
-		offset += rsp->data_len - 1;
115784
-		
115784
-		/*
115784
-		 * Increment our list for the next call
115784
-		 */
115784
-		++list_index;
115784
-		rqdata[2] =  (rqdata[2] & 0x80) + list_index; 
115784
 
115784
+	do {
115784
+		/* Always ask for cipher suite format */
115784
+		rqdata[2] = LIST_ALGORITHMS_BY_CIPHER_SUITE | list_index;
115784
 		rsp = intf->sendrecv(intf, &req;;
115784
 		if (rsp == NULL) {
115784
 			lprintf(LOG_ERR, "Unable to Get Channel Cipher Suites");
115784
 			return -1;
115784
 		}
115784
-		if (rsp->ccode > 0) {
115784
+		if (rsp->ccode || rsp->data_len < 1) {
115784
 			lprintf(LOG_ERR, "Get Channel Cipher Suites failed: %s",
115784
 					val2str(rsp->ccode, completion_code_vals));
115784
 			return -1;
115784
 		}
115784
-	}
115784
-
115784
-	/* Copy last chunk */
115784
-	if(rsp->data_len > 1) {
115784
 		/*
115784
 		 * We got back cipher suite data -- store it.
115784
 		 * printf("copying data to offset %d\n", offset);
115784
@@ -429,88 +459,46 @@ ipmi_get_channel_cipher_suites(struct ipmi_intf *intf, const char *payload_type,
115784
 		 */
115784
 		memcpy(cipher_suite_data + offset, rsp->data + 1, rsp->data_len - 1);
115784
 		offset += rsp->data_len - 1;
115784
-	}
115784
 
115784
-	/* We can chomp on all our data now. */
115784
-	cipher_suite_data_length = offset;
115784
-	offset = 0;
115784
+		/*
115784
+		 * Increment our list for the next call
115784
+		 */
115784
+		++list_index;
115784
+	} while ((rsp->data_len == (sizeof(uint8_t) + MAX_CIPHER_SUITE_DATA_LEN)) &&
115784
+			 (list_index < MAX_CIPHER_SUITE_RECORD_OFFSET));
115784
 
115784
-	if (! csv_output) {
115784
-		printf("ID   IANA    Auth Alg        Integrity Alg   Confidentiality Alg\n");
115784
-	}
115784
-	while (offset < cipher_suite_data_length) {
115784
-		if (cipher_suite_data[offset++] == 0xC0) {
115784
-			/* standard type */
115784
-			iana = 0;
115784
+	*count = parse_channel_cipher_suite_data(cipher_suite_data, offset, suites,
115784
+	                                         nr_suites);
115784
+	return 0;
115784
+}
115784
 
115784
-			/* Verify that we have at least a full record left; id + 3 algs */
115784
-			if ((cipher_suite_data_length - offset) < 4) {
115784
-				lprintf(LOG_ERR, "Incomplete data record in cipher suite data");
115784
-				return -1;
115784
-			}
115784
-			cipher_suite_id = cipher_suite_data[offset++];
115784
-		} else if (cipher_suite_data[offset++] == 0xC1) {
115784
-			/* OEM record type */
115784
-			/* Verify that we have at least a full record left
115784
-			 * id + iana + 3 algs
115784
-			 */
115784
-			if ((cipher_suite_data_length - offset) < 4) {
115784
-				lprintf(LOG_ERR, "Incomplete data record in cipher suite data");
115784
-				return -1;
115784
-			}
115784
+static int
115784
+ipmi_print_channel_cipher_suites(struct ipmi_intf *intf, const char *payload_type,
115784
+		uint8_t channel)
115784
+{
115784
+	int rc;
115784
+	size_t i = 0;
115784
+	struct cipher_suite_info suites[MAX_CIPHER_SUITE_COUNT];
115784
+	size_t nr_suites = sizeof(*suites);
115784
 
115784
-			cipher_suite_id = cipher_suite_data[offset++];
115784
+	rc = ipmi_get_channel_cipher_suites(intf, payload_type, channel,
115784
+			suites, &nr_suites);
115784
 
115784
-			/* Grab the IANA */
115784
-			iana =
115784
-				cipher_suite_data[offset]            | 
115784
-				(cipher_suite_data[offset + 1] << 8) | 
115784
-				(cipher_suite_data[offset + 2] << 16);
115784
-			offset += 3;
115784
-		} else {
115784
-			lprintf(LOG_ERR, "Bad start of record byte in cipher suite data");
115784
-			return -1;
115784
-		}
115784
+	if (rc < 0)
115784
+		return rc;
115784
 
115784
-		/*
115784
-		 * Grab the algorithms for this cipher suite.  I guess we can't be
115784
-		 * sure of what order they'll come in.  Also, I suppose we default
115784
-		 * to the NONE algorithm if one were absent.  This part of the spec is
115784
-		 * poorly written -- I have read the errata document.  For now, I'm only
115784
-		 * allowing one algorithm per type (auth, integrity, crypt) because I
115784
-		 * don't I understand how it could be otherwise.
115784
-		 */
115784
-		auth_alg      = IPMI_AUTH_RAKP_NONE;
115784
-		integrity_alg = IPMI_INTEGRITY_NONE;
115784
-		crypt_alg     = IPMI_CRYPT_NONE;
115784
-		
115784
-		while (((cipher_suite_data[offset] & 0xC0) != 0xC0) &&
115784
-			   ((cipher_suite_data_length - offset) > 0))
115784
-		{
115784
-			switch (cipher_suite_data[offset] & 0xC0)
115784
-			{
115784
-			case 0x00:
115784
-				/* Authentication algorithm specifier */
115784
-				auth_alg = cipher_suite_data[offset++] & 0x3F;
115784
-				break;
115784
-			case 0x40:
115784
-				/* Interity algorithm specifier */
115784
-				integrity_alg = cipher_suite_data[offset++] & 0x3F;
115784
-				break;
115784
-			case 0x80:
115784
-				/* Confidentiality algorithm specifier */
115784
-				crypt_alg = cipher_suite_data[offset++] & 0x3F;
115784
-				break;
115784
-			}
115784
-		}
115784
+	if (! csv_output) {
115784
+		printf("ID   IANA    Auth Alg        Integrity Alg   Confidentiality Alg\n");
115784
+	}
115784
+	for (i = 0; i < nr_suites; i++) {
115784
 		/* We have everything we need to spit out a cipher suite record */
115784
 		printf((csv_output? "%d,%s,%s,%s,%s\n" :
115784
 			"%-4d %-7s %-15s %-15s %-15s\n"),
115784
-		       cipher_suite_id,
115784
-		       iana_string(iana),
115784
-		       val2str(auth_alg, ipmi_auth_algorithms),
115784
-		       val2str(integrity_alg, ipmi_integrity_algorithms),
115784
-		       val2str(crypt_alg, ipmi_encryption_algorithms));
115784
+		       suites[i].cipher_suite_id,
115784
+		       iana_string(suites[i].iana),
115784
+		       val2str(suites[i].auth_alg, ipmi_auth_algorithms),
115784
+		       val2str(suites[i].integrity_alg, ipmi_integrity_algorithms),
115784
+		       val2str(suites[i].crypt_alg, ipmi_encryption_algorithms));
115784
 	}
115784
 	return 0;
115784
 }
115784
@@ -973,7 +961,7 @@ ipmi_channel_main(struct ipmi_intf *intf, int argc, char **argv)
115784
 				return (-1);
115784
 			}
115784
 		}
115784
-		retval = ipmi_get_channel_cipher_suites(intf,
115784
+		retval = ipmi_print_channel_cipher_suites(intf,
115784
 							argv[1], /* ipmi | sol */
115784
 							channel);
115784
 	} else if (strncmp(argv[0], "setkg", 5) == 0) {
115784
diff --git a/lib/ipmi_main.c b/lib/ipmi_main.c
115784
index 811c80b..6aee102 100644
115784
--- a/lib/ipmi_main.c
115784
+++ b/lib/ipmi_main.c
115784
@@ -323,6 +323,7 @@ ipmi_main(int argc, char ** argv,
115784
 	uint8_t target_addr = 0;
115784
 	uint8_t target_channel = 0;
115784
 
115784
+	uint8_t u8tmp = 0;
115784
 	uint8_t transit_addr = 0;
115784
 	uint8_t transit_channel = 0;
115784
 	uint8_t target_lun     = 0;
115784
@@ -347,7 +348,10 @@ ipmi_main(int argc, char ** argv,
115784
 	char * seloem   = NULL;
115784
 	int port = 0;
115784
 	int devnum = 0;
115784
-	int cipher_suite_id = 3; /* See table 22-19 of the IPMIv2 spec */
115784
+#ifdef IPMI_INTF_LANPLUS
115784
+	/* lookup best cipher suite available */
115784
+	enum cipher_suite_ids cipher_suite_id = IPMI_LANPLUS_CIPHER_SUITE_RESERVED;
115784
+#endif /* IPMI_INTF_LANPLUS */
115784
 	int argflag, i, found;
115784
 	int rc = -1;
115784
 	int ai_family = AF_UNSPEC;
115784
@@ -425,19 +429,18 @@ ipmi_main(int argc, char ** argv,
115784
 				goto out_free;
115784
 			}
115784
 			break;
115784
+#ifdef IPMI_INTF_LANPLUS
115784
 		case 'C':
115784
-			if (str2int(optarg, &cipher_suite_id) != 0) {
115784
-				lprintf(LOG_ERR, "Invalid parameter given or out of range for '-C'.");
115784
-				rc = -1;
115784
-				goto out_free;
115784
-			}
115784
-			/* add check Cipher is -gt 0 */
115784
-			if (cipher_suite_id < 0) {
115784
-				lprintf(LOG_ERR, "Cipher suite ID %i is invalid.", cipher_suite_id);
115784
+			/* Cipher Suite ID is a byte as per IPMI specification */
115784
+			if (str2uchar(optarg, &u8tmp) != 0) {
115784
+				lprintf(LOG_ERR, "Invalid parameter given or out of "
115784
+				                 "range [0-255] for '-C'.");
115784
 				rc = -1;
115784
 				goto out_free;
115784
 			}
115784
+			cipher_suite_id = u8tmp;
115784
 			break;
115784
+#endif /* IPMI_INTF_LANPLUS */
115784
 		case 'v':
115784
 			verbose++;
115784
 			break;
115784
@@ -870,7 +873,9 @@ ipmi_main(int argc, char ** argv,
115784
 
115784
 	ipmi_intf_session_set_lookupbit(ipmi_main_intf, lookupbit);
115784
 	ipmi_intf_session_set_sol_escape_char(ipmi_main_intf, sol_escape_char);
115784
+#ifdef IPMI_INTF_LANPLUS
115784
 	ipmi_intf_session_set_cipher_suite_id(ipmi_main_intf, cipher_suite_id);
115784
+#endif /* IPMI_INTF_LANPLUS */
115784
 
115784
 	ipmi_main_intf->devnum = devnum;
115784
 
115784
diff --git a/src/plugins/ipmi_intf.c b/src/plugins/ipmi_intf.c
115784
index 1d9e87b..00b0918 100644
115784
--- a/src/plugins/ipmi_intf.c
115784
+++ b/src/plugins/ipmi_intf.c
115784
@@ -252,11 +252,14 @@ ipmi_intf_session_set_lookupbit(struct ipmi_intf * intf, uint8_t lookupbit)
115784
 	intf->ssn_params.lookupbit = lookupbit;
115784
 }
115784
 
115784
+#ifdef IPMI_INTF_LANPLUS
115784
 void
115784
-ipmi_intf_session_set_cipher_suite_id(struct ipmi_intf * intf, uint8_t cipher_suite_id)
115784
+ipmi_intf_session_set_cipher_suite_id(struct ipmi_intf * intf,
115784
+		enum cipher_suite_ids cipher_suite_id)
115784
 {
115784
 	intf->ssn_params.cipher_suite_id = cipher_suite_id;
115784
 }
115784
+#endif /* IPMI_INTF_LANPLUS */
115784
 
115784
 void
115784
 ipmi_intf_session_set_sol_escape_char(struct ipmi_intf * intf, char sol_escape_char)
115784
diff --git a/src/plugins/lanplus/lanplus.c b/src/plugins/lanplus/lanplus.c
115784
index a0e388c..3087348 100644
115784
--- a/src/plugins/lanplus/lanplus.c
115784
+++ b/src/plugins/lanplus/lanplus.c
115784
@@ -164,114 +164,109 @@ extern int verbose;
115784
  * returns 0 on success
115784
  *         1 on failure
115784
  */
115784
-int lanplus_get_requested_ciphers(int       cipher_suite_id,
115784
+int lanplus_get_requested_ciphers(enum cipher_suite_ids cipher_suite_id,
115784
 								  uint8_t * auth_alg,
115784
 								  uint8_t * integrity_alg,
115784
 								  uint8_t * crypt_alg)
115784
 {
115784
-#ifdef HAVE_CRYPTO_SHA256
115784
-	if ((cipher_suite_id < 0) || (cipher_suite_id > 17)) {
115784
-		return 1;
115784
-	}
115784
-#else
115784
-	if ((cipher_suite_id < 0) || (cipher_suite_id > 14))
115784
-		return 1;
115784
-#endif /* HAVE_CRYPTO_SHA256 */
115784
 		/* See table 22-19 for the source of the statement */
115784
 	switch (cipher_suite_id)
115784
 	{
115784
-	case 0:
115784
+	case IPMI_LANPLUS_CIPHER_SUITE_0:
115784
 		*auth_alg      = IPMI_AUTH_RAKP_NONE;
115784
 		*integrity_alg = IPMI_INTEGRITY_NONE;
115784
 		*crypt_alg     = IPMI_CRYPT_NONE;
115784
 		break;
115784
-	case 1:
115784
+	case IPMI_LANPLUS_CIPHER_SUITE_1:
115784
 		*auth_alg      = IPMI_AUTH_RAKP_HMAC_SHA1;
115784
 		*integrity_alg = IPMI_INTEGRITY_NONE;
115784
 		*crypt_alg     = IPMI_CRYPT_NONE;
115784
 		break;
115784
-	case 2:
115784
+	case IPMI_LANPLUS_CIPHER_SUITE_2:
115784
 		*auth_alg      = IPMI_AUTH_RAKP_HMAC_SHA1;
115784
 		*integrity_alg = IPMI_INTEGRITY_HMAC_SHA1_96;
115784
 		*crypt_alg     = IPMI_CRYPT_NONE;
115784
 		break;
115784
-	case 3:
115784
+	case IPMI_LANPLUS_CIPHER_SUITE_3:
115784
 		*auth_alg      = IPMI_AUTH_RAKP_HMAC_SHA1;
115784
 		*integrity_alg = IPMI_INTEGRITY_HMAC_SHA1_96;
115784
 		*crypt_alg     = IPMI_CRYPT_AES_CBC_128;
115784
 		break;
115784
-	case 4:
115784
+	case IPMI_LANPLUS_CIPHER_SUITE_4:
115784
 		*auth_alg      = IPMI_AUTH_RAKP_HMAC_SHA1;
115784
 		*integrity_alg = IPMI_INTEGRITY_HMAC_SHA1_96;
115784
 		*crypt_alg     = IPMI_CRYPT_XRC4_128;
115784
 		break;
115784
-	case 5:
115784
+	case IPMI_LANPLUS_CIPHER_SUITE_5:
115784
 		*auth_alg      = IPMI_AUTH_RAKP_HMAC_SHA1;
115784
 		*integrity_alg = IPMI_INTEGRITY_HMAC_SHA1_96;
115784
 		*crypt_alg     = IPMI_CRYPT_XRC4_40;
115784
 		break;
115784
-	case 6:
115784
+	case IPMI_LANPLUS_CIPHER_SUITE_6:
115784
 		*auth_alg      = IPMI_AUTH_RAKP_HMAC_MD5;
115784
 		*integrity_alg = IPMI_INTEGRITY_NONE;
115784
 		*crypt_alg     = IPMI_CRYPT_NONE;
115784
 		break;
115784
-	case 7:
115784
+	case IPMI_LANPLUS_CIPHER_SUITE_7:
115784
 		*auth_alg      = IPMI_AUTH_RAKP_HMAC_MD5;
115784
 		*integrity_alg = IPMI_INTEGRITY_HMAC_MD5_128;
115784
 		*crypt_alg     = IPMI_CRYPT_NONE;
115784
 		break;
115784
-	case 8:
115784
+	case IPMI_LANPLUS_CIPHER_SUITE_8:
115784
 		*auth_alg      = IPMI_AUTH_RAKP_HMAC_MD5;
115784
 		*integrity_alg = IPMI_INTEGRITY_HMAC_MD5_128;
115784
 		*crypt_alg     = IPMI_CRYPT_AES_CBC_128;
115784
 		break;
115784
-	case 9:
115784
+	case IPMI_LANPLUS_CIPHER_SUITE_9:
115784
 		*auth_alg      = IPMI_AUTH_RAKP_HMAC_MD5;
115784
 		*integrity_alg = IPMI_INTEGRITY_HMAC_MD5_128;
115784
 		*crypt_alg     = IPMI_CRYPT_XRC4_128;
115784
 		break;
115784
-	case 10:
115784
+	case IPMI_LANPLUS_CIPHER_SUITE_10:
115784
 		*auth_alg      = IPMI_AUTH_RAKP_HMAC_MD5;
115784
 		*integrity_alg = IPMI_INTEGRITY_HMAC_MD5_128;
115784
 		*crypt_alg     = IPMI_CRYPT_XRC4_40;
115784
 		break;
115784
-	case 11:
115784
+	case IPMI_LANPLUS_CIPHER_SUITE_11:
115784
 		*auth_alg      = IPMI_AUTH_RAKP_HMAC_MD5;
115784
 		*integrity_alg = IPMI_INTEGRITY_MD5_128;
115784
 		*crypt_alg     = IPMI_CRYPT_NONE;
115784
 		break;
115784
-	case 12:
115784
+	case IPMI_LANPLUS_CIPHER_SUITE_12:
115784
 		*auth_alg      = IPMI_AUTH_RAKP_HMAC_MD5;
115784
 		*integrity_alg = IPMI_INTEGRITY_MD5_128;
115784
 		*crypt_alg     = IPMI_CRYPT_AES_CBC_128;
115784
 		break;
115784
-	case 13:
115784
+	case IPMI_LANPLUS_CIPHER_SUITE_13:
115784
 		*auth_alg      = IPMI_AUTH_RAKP_HMAC_MD5;
115784
 		*integrity_alg = IPMI_INTEGRITY_MD5_128;
115784
 		*crypt_alg     = IPMI_CRYPT_XRC4_128;
115784
 		break;
115784
-	case 14:
115784
+	case IPMI_LANPLUS_CIPHER_SUITE_14:
115784
 		*auth_alg      = IPMI_AUTH_RAKP_HMAC_MD5;
115784
 		*integrity_alg = IPMI_INTEGRITY_MD5_128;
115784
 		*crypt_alg     = IPMI_CRYPT_XRC4_40;
115784
 		break;
115784
 #ifdef HAVE_CRYPTO_SHA256
115784
-	case 15:
115784
+	case IPMI_LANPLUS_CIPHER_SUITE_15:
115784
 		*auth_alg      = IPMI_AUTH_RAKP_HMAC_SHA256;
115784
 		*integrity_alg = IPMI_INTEGRITY_NONE;
115784
 		*crypt_alg     = IPMI_CRYPT_NONE;
115784
 		break;
115784
-	case 16:
115784
+	case IPMI_LANPLUS_CIPHER_SUITE_16:
115784
 		*auth_alg      = IPMI_AUTH_RAKP_HMAC_SHA256;
115784
 		*integrity_alg = IPMI_INTEGRITY_HMAC_SHA256_128;
115784
 		*crypt_alg     = IPMI_CRYPT_NONE;
115784
 		break;
115784
-	case 17:
115784
+	case IPMI_LANPLUS_CIPHER_SUITE_17:
115784
 		*auth_alg      = IPMI_AUTH_RAKP_HMAC_SHA256;
115784
 		*integrity_alg = IPMI_INTEGRITY_HMAC_SHA256_128;
115784
 		*crypt_alg     = IPMI_CRYPT_AES_CBC_128;
115784
 		break;
115784
 #endif /* HAVE_CRYPTO_SHA256 */
115784
+	case IPMI_LANPLUS_CIPHER_SUITE_RESERVED:
115784
+	default:
115784
+		return 1;
115784
 	}
115784
 
115784
 	return 0;
115784
@@ -3441,6 +3436,57 @@ ipmi_set_session_privlvl_cmd(struct ipmi_intf * intf)
115784
 	return 0;
115784
 }
115784
 
115784
+static uint8_t
115784
+ipmi_find_best_cipher_suite(struct ipmi_intf *intf)
115784
+{
115784
+	enum cipher_suite_ids best_suite = IPMI_LANPLUS_CIPHER_SUITE_RESERVED;
115784
+#ifdef HAVE_CRYPTO_SHA256
115784
+	struct cipher_suite_info suites[MAX_CIPHER_SUITE_COUNT];
115784
+	size_t nr_suites = ARRAY_SIZE(suites);
115784
+	/* cipher suite best order is chosen with this criteria:
115784
+	 * HMAC-MD5 and MD5 are BAD; xRC4 is bad; AES128 is required
115784
+	 * HMAC-SHA256 > HMAC-SHA1
115784
+	 * secure authentication > encrypted content
115784
+	 *
115784
+	 * With xRC4 out, all cipher suites with MD5 out, and cipher suite 3 being
115784
+	 * required by the spec, the only better defined standard cipher suite is
115784
+	 * 17. So if SHA256 is available, we should try to use that, otherwise,
115784
+	 * fall back to 3.
115784
+	 */
115784
+	const enum cipher_suite_ids cipher_order_preferred[] = {
115784
+		IPMI_LANPLUS_CIPHER_SUITE_17,
115784
+		IPMI_LANPLUS_CIPHER_SUITE_3,
115784
+	};
115784
+	const size_t nr_preferred = ARRAY_SIZE(cipher_order_preferred);
115784
+	size_t ipref, i;
115784
+
115784
+	if (ipmi_get_channel_cipher_suites(intf, "ipmi", IPMI_LAN_CHANNEL_E,
115784
+	                                   suites, &nr_suites) < 0)
115784
+	{
115784
+		/* default legacy behavior - cipher suite 3 if none is requested */
115784
+		return IPMI_LANPLUS_CIPHER_SUITE_3;
115784
+	}
115784
+	for (ipref = 0; ipref < nr_preferred &&
115784
+	                IPMI_LANPLUS_CIPHER_SUITE_RESERVED == best_suite; ipref++)
115784
+	{
115784
+		for (i = 0; i < nr_suites; i++) {
115784
+			if (cipher_order_preferred[ipref] == suites[i].cipher_suite_id) {
115784
+				best_suite = cipher_order_preferred[ipref];
115784
+				break;
115784
+			}
115784
+		}
115784
+	}
115784
+#endif /* HAVE_CRYPTO_SHA256 */
115784
+	if (IPMI_LANPLUS_CIPHER_SUITE_RESERVED == best_suite) {
115784
+		/* IPMI 2.0 spec requires that cipher suite 3 is implemented
115784
+		 * so we should always be able to fall back to that if better
115784
+		 * options are not available. */
115784
+		best_suite = IPMI_LANPLUS_CIPHER_SUITE_3;
115784
+	}
115784
+	lprintf(LOG_INFO, "Using best available cipher suite %d\n", best_suite);
115784
+	return best_suite;
115784
+}
115784
+
115784
 /**
115784
  * ipmi_lanplus_open
115784
  */
115784
@@ -3514,6 +3560,16 @@ ipmi_lanplus_open(struct ipmi_intf * intf)
115784
 		lprintf(LOG_INFO, "This BMC does not support IPMI v2 / RMCP+");
115784
 		goto fail;
115784
 	}
115784
+	/*
115784
+	 * If no cipher suite was provided, query the channel cipher suite list and
115784
+	 * pick the best one available
115784
+	 */
115784
+	if (IPMI_LANPLUS_CIPHER_SUITE_RESERVED ==
115784
+	    intf->ssn_params.cipher_suite_id)
115784
+	{
115784
+		ipmi_intf_session_set_cipher_suite_id(intf,
115784
+			ipmi_find_best_cipher_suite(intf));
115784
+	}
115784
 
115784
 	/*
115784
 	 * If the open/rakp1/rakp3 sequence encounters a timeout, the whole sequence
115784
@@ -3728,7 +3784,7 @@ static int ipmi_lanplus_setup(struct ipmi_intf * intf)
115784
 
115784
 static void ipmi_lanp_set_max_rq_data_size(struct ipmi_intf * intf, uint16_t size)
115784
 {
115784
-	if (intf->ssn_params.cipher_suite_id == 3) {
115784
+	if (intf->ssn_params.cipher_suite_id == IPMI_LANPLUS_CIPHER_SUITE_3) {
115784
 		/*
115784
 		 * encrypted payload can only be multiple of 16 bytes
115784
 		 */
115784
@@ -3746,7 +3802,7 @@ static void ipmi_lanp_set_max_rq_data_size(struct ipmi_intf * intf, uint16_t siz
115784
 
115784
 static void ipmi_lanp_set_max_rp_data_size(struct ipmi_intf * intf, uint16_t size)
115784
 {
115784
-	if (intf->ssn_params.cipher_suite_id == 3) {
115784
+	if (intf->ssn_params.cipher_suite_id == IPMI_LANPLUS_CIPHER_SUITE_3) {
115784
 		/*
115784
 		 * encrypted payload can only be multiple of 16 bytes
115784
 		 */
115784
-- 
115784
2.20.1
115784