View Javadoc
1   package org.metricshub.ipmi.core.coding.commands.session;
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.commands.CommandCodes;
26  import org.metricshub.ipmi.core.coding.commands.IpmiCommandCoder;
27  import org.metricshub.ipmi.core.coding.commands.IpmiVersion;
28  import org.metricshub.ipmi.core.coding.commands.ResponseData;
29  import org.metricshub.ipmi.core.coding.payload.lan.IPMIException;
30  import org.metricshub.ipmi.core.coding.payload.lan.IpmiLanMessage;
31  import org.metricshub.ipmi.core.coding.payload.lan.IpmiLanRequest;
32  import org.metricshub.ipmi.core.coding.payload.lan.NetworkFunction;
33  import org.metricshub.ipmi.core.coding.protocol.AuthenticationType;
34  import org.metricshub.ipmi.core.coding.protocol.IpmiMessage;
35  import org.metricshub.ipmi.core.coding.protocol.Ipmiv20Message;
36  import org.metricshub.ipmi.core.coding.protocol.PayloadType;
37  import org.metricshub.ipmi.core.coding.security.CipherSuite;
38  import org.metricshub.ipmi.core.coding.security.ConfidentialityNone;
39  import org.metricshub.ipmi.core.common.TypeConverter;
40  
41  import java.security.InvalidKeyException;
42  import java.security.NoSuchAlgorithmException;
43  
44  /**
45   * Wrapper for RMCP+ Get Channel Cipher Suites command. This command can be
46   * executed prior to establishing a session with the BMC.
47   */
48  public class GetChannelCipherSuites extends IpmiCommandCoder {
49  
50      private byte channelNumber;
51  
52      private byte index;
53  
54      /**
55       * Sets the channel number that will be put into IPMI command.
56       *
57       * @param channelNumber
58       *            - must be 0h-Bh or Eh-Fh <br>
59       *            Eh = retrieve information for channel this request was issued
60       *            on
61       * @throws IllegalArgumentException
62       */
63      public void setChannelNumber(int channelNumber) {
64          if (channelNumber < 0 || channelNumber > 0xF || channelNumber == 0xC
65                  || channelNumber == 0xD) {
66              throw new IllegalArgumentException("Invalid channel number");
67          }
68          this.channelNumber = TypeConverter.intToByte(channelNumber);
69      }
70  
71      public int getChannelNumber() {
72          return TypeConverter.byteToInt(channelNumber);
73      }
74  
75      public void setIndex(byte index) {
76          if (index > 0x3F || index < 0) {
77              throw new IllegalArgumentException("Index " + index + " invalid must be (00h-3Fh).");
78          }
79          this.index = index;
80      }
81  
82      public byte getIndex() {
83          return index;
84      }
85  
86      /**
87       * Initiates class for decoding.
88       */
89      public GetChannelCipherSuites() {
90          super(IpmiVersion.V20, new CipherSuite((byte) 0, (byte) 0, (byte) 0,
91                  (byte) 0), AuthenticationType.RMCPPlus);
92      }
93  
94      /**
95       * Initiates class for both encoding and decoding.
96       *
97       * @param channelNumber
98       *            - must be 0h-Bh or Eh-Fh <br>
99       *            Eh = retrieve information for channel this request was issued
100      *            on
101      * @param index
102      *            - (00h-3Fh). 0h selects the first set of 16 cipher suites, 1h
103      *            selects the next set of 16, and so on
104      */
105     public GetChannelCipherSuites(byte channelNumber, byte index) {
106         super(IpmiVersion.V20, new CipherSuite((byte) 0, (byte) 0, (byte) 0,
107                 (byte) 0), AuthenticationType.RMCPPlus);
108         setChannelNumber(channelNumber);
109         setIndex(index);
110     }
111 
112     @Override
113     public IpmiMessage encodePayload(int messageSequenceNumber, int sessionSequenceNumber, int sessionId)
114             throws NoSuchAlgorithmException, InvalidKeyException {
115         Ipmiv20Message message = new Ipmiv20Message(new ConfidentialityNone());
116 
117         message.setAuthenticationType(getAuthenticationType());
118 
119         message.setSessionID(0);
120 
121         message.setPayloadEncrypted(false);
122 
123         message.setPayloadAuthenticated(false);
124 
125         message.setSessionSequenceNumber(0);
126 
127         message.setPayloadType(PayloadType.Ipmi);
128 
129         message.setPayload(preparePayload(messageSequenceNumber));
130 
131         return message;
132     }
133 
134     @Override
135     protected IpmiLanMessage preparePayload(int sequenceNumber) {
136         byte[] requestData = new byte[3];
137 
138         requestData[0] = channelNumber;
139 
140         requestData[1] = 0; // payload type = IPMI
141 
142         requestData[2] = TypeConverter.intToByte(0x80 | getIndex());
143 
144         return new IpmiLanRequest(getNetworkFunction(), getCommandCode(),
145                 requestData, TypeConverter.intToByte(sequenceNumber));
146     }
147 
148     @Override
149     public byte getCommandCode() {
150         return CommandCodes.GET_CHANNEL_CIPHER_SUITES;
151     }
152 
153     @Override
154     public NetworkFunction getNetworkFunction() {
155         return NetworkFunction.ApplicationRequest;
156     }
157 
158     @Override
159     public ResponseData getResponseData(IpmiMessage message) throws IPMIException, NoSuchAlgorithmException, InvalidKeyException {
160 
161         GetChannelCipherSuitesResponseData data = new GetChannelCipherSuitesResponseData();
162 
163         byte[] raw = message.getPayload().getIpmiCommandData();
164 
165         data.setChannelNumber(raw[0]);
166 
167         if (raw.length > 1) {
168             byte[] cssData = new byte[raw.length - 1];
169 
170             System.arraycopy(raw, 1, cssData, 0, cssData.length);
171 
172             data.setCipherSuiteData(cssData);
173         } else if(raw.length == 1) {
174             data.setCipherSuiteData(new byte[0]);
175         }
176 
177         return data;
178     }
179 
180 }