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 message.setAuthenticationType(decodeAuthenticationType(raw[0]));
59
60 int offset = 1;
61
62 message.setSessionSequenceNumber(decodeSessionSequenceNumber(raw,
63 offset));
64
65 offset += 4;
66
67 message.setSessionID(decodeSessionID(raw, offset));
68
69 offset += 4;
70
71 if (message.getAuthenticationType() != AuthenticationType.None) {
72 message.setAuthCode(decodeAuthCode(raw, offset));
73 offset += 16;
74 }
75
76 int payloadLength = decodePayloadLength(raw, offset);
77
78 message.setPayloadLength(payloadLength);
79 ++offset;
80
81 message.setPayload(decodePayload(raw, offset, payloadLength,
82 message.getConfidentialityAlgorithm(), PayloadType.Ipmi));
83
84 return message;
85 }
86
87 /**
88 * Decodes authentication code.
89 *
90 * @param rawMessage
91 * - Byte array holding whole message data.
92 * @param offset
93 * - Offset to authentication code in header.
94 * @return authentication code.
95 */
96 private byte[] decodeAuthCode(byte[] rawMessage, int offset) {
97 byte[] authCode = new byte[16];
98
99 System.arraycopy(rawMessage, offset, authCode, 0, 16);
100
101 return authCode;
102 }
103
104 @Override
105 protected int decodePayloadLength(byte[] rawData, int offset) {
106 return TypeConverter.byteToInt(rawData[offset]);
107 }
108 }