View Javadoc
1   package org.metricshub.ipmi.core.coding.protocol.decoder;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * IPMI Java Client
6    * ჻჻჻჻჻჻
7    * Copyright 2023 Verax Systems, MetricsHub
8    * ჻჻჻჻჻჻
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
23   */
24  
25  import org.metricshub.ipmi.core.coding.protocol.AuthenticationType;
26  import org.metricshub.ipmi.core.coding.protocol.IpmiMessage;
27  import org.metricshub.ipmi.core.coding.protocol.Ipmiv15Message;
28  import org.metricshub.ipmi.core.coding.protocol.PayloadType;
29  import org.metricshub.ipmi.core.coding.rmcp.RmcpMessage;
30  import org.metricshub.ipmi.core.common.TypeConverter;
31  
32  /**
33   * Decodes IPMI v1.5 session header and retrieves encrypted payload.
34   */
35  public class Protocolv15Decoder extends ProtocolDecoder {
36  
37  	public Protocolv15Decoder() {
38  		super();
39  	}
40  
41  	/**
42  	 * Decodes IPMI v1.5 message fields.
43  	 *
44  	 * @param rmcpMessage
45  	 *        - RMCP message to decode.
46  	 * @return decoded message
47  	 * @see Ipmiv15Message
48  	 * @throws IllegalArgumentException
49  	 *         when delivered RMCP message does not contain encapsulated
50  	 *         IPMI message.
51  	 */
52  	@Override
53  	public IpmiMessage decode(RmcpMessage rmcpMessage) {
54  		Ipmiv15Message message = new Ipmiv15Message();
55  
56  		byte[] raw = rmcpMessage.getData();
57  
58  		// Auth Type, Session Sequence Number, Session ID, [AuthCode,] Message Length
59  		if (raw.length < 10) {
60  			throw new IllegalArgumentException("Message is truncated");
61  		}
62  
63  		message.setAuthenticationType(decodeAuthenticationType(raw[0]));
64  
65  		int offset = 1;
66  
67  		message.setSessionSequenceNumber(decodeSessionSequenceNumber(raw, offset));
68  
69  		offset += 4;
70  
71  		message.setSessionID(decodeSessionID(raw, offset));
72  
73  		offset += 4;
74  
75  		if (message.getAuthenticationType() != AuthenticationType.None) {
76  			if (raw.length < 26) {
77  				throw new IllegalArgumentException("Message is truncated");
78  			}
79  			message.setAuthCode(decodeAuthCode(raw, offset));
80  			offset += 16;
81  		}
82  
83  		int payloadLength = decodePayloadLength(raw, offset);
84  
85  		message.setPayloadLength(payloadLength);
86  		++offset;
87  
88  		if (raw.length < offset + payloadLength) {
89  			throw new IllegalArgumentException("Message is truncated");
90  		}
91  
92  		message
93  				.setPayload(decodePayload(raw, offset, payloadLength, message.getConfidentialityAlgorithm(), PayloadType.Ipmi));
94  
95  		return message;
96  	}
97  
98  	/**
99  	 * Decodes authentication code.
100 	 *
101 	 * @param rawMessage
102 	 *        - Byte array holding whole message data.
103 	 * @param offset
104 	 *        - Offset to authentication code in header.
105 	 * @return authentication code.
106 	 */
107 	private byte[] decodeAuthCode(byte[] rawMessage, int offset) {
108 		byte[] authCode = new byte[16];
109 
110 		System.arraycopy(rawMessage, offset, authCode, 0, 16);
111 
112 		return authCode;
113 	}
114 
115 	@Override
116 	protected int decodePayloadLength(byte[] rawData, int offset) {
117 		return TypeConverter.byteToInt(rawData[offset]);
118 	}
119 }