View Javadoc
1   package org.metricshub.ipmi.core.sm.states;
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.Encoder;
26  import org.metricshub.ipmi.core.coding.commands.session.GetChannelCipherSuites;
27  import org.metricshub.ipmi.core.coding.payload.lan.IpmiLanResponse;
28  import org.metricshub.ipmi.core.coding.protocol.AuthenticationType;
29  import org.metricshub.ipmi.core.coding.protocol.IpmiMessage;
30  import org.metricshub.ipmi.core.coding.protocol.PayloadType;
31  import org.metricshub.ipmi.core.coding.protocol.decoder.ProtocolDecoder;
32  import org.metricshub.ipmi.core.coding.protocol.decoder.Protocolv20Decoder;
33  import org.metricshub.ipmi.core.coding.protocol.encoder.Protocolv20Encoder;
34  import org.metricshub.ipmi.core.coding.rmcp.RmcpMessage;
35  import org.metricshub.ipmi.core.coding.security.CipherSuite;
36  import org.metricshub.ipmi.core.common.TypeConverter;
37  import org.metricshub.ipmi.core.sm.StateMachine;
38  import org.metricshub.ipmi.core.sm.actions.ErrorAction;
39  import org.metricshub.ipmi.core.sm.actions.ResponseAction;
40  import org.metricshub.ipmi.core.sm.events.DefaultAck;
41  import org.metricshub.ipmi.core.sm.events.GetChannelCipherSuitesPending;
42  import org.metricshub.ipmi.core.sm.events.StateMachineEvent;
43  import org.metricshub.ipmi.core.sm.events.Timeout;
44  
45  /**
46   * State at which getting Channel Cipher Suites is in progress. Transits back to
47   * {@link Uninitialized} on {@link Timeout}, further proceeds with getting
48   * Cipher Suites on {@link GetChannelCipherSuitesPending} and moves on to
49   * {@link Ciphers} on {@link DefaultAck}
50   */
51  public class CiphersWaiting extends State {
52  
53      private int index;
54  
55      private int tag;
56  
57      /**
58       * Initializes state.
59       *
60       * @param index
61       *            - Index of the channel cipher suite package to get
62       * @param tag
63       *            - Tag of the message
64       */
65      public CiphersWaiting(int index, int tag) {
66          this.index = index;
67          this.tag = tag;
68      }
69  
70      @Override
71      public void doTransition(StateMachine stateMachine,
72              StateMachineEvent machineEvent) {
73          if (machineEvent instanceof Timeout) {
74              stateMachine.setCurrent(new Uninitialized());
75          } else if (machineEvent instanceof GetChannelCipherSuitesPending) {
76              GetChannelCipherSuitesPending event = (GetChannelCipherSuitesPending) machineEvent;
77              GetChannelCipherSuites cipherSuites = new GetChannelCipherSuites(
78                      TypeConverter.intToByte(0xE),
79                      TypeConverter.intToByte(index + 1));
80              try {
81                  tag = event.getSequenceNumber();
82                  stateMachine.sendMessage(Encoder.encode(
83                          new Protocolv20Encoder(), cipherSuites,
84                          event.getSequenceNumber(), 0, 0));
85                  ++index;
86              } catch (Exception e) {
87                  stateMachine.doExternalAction(new ErrorAction(e));
88              }
89          } else if (machineEvent instanceof DefaultAck) {
90              stateMachine.setCurrent(new Ciphers());
91          } else {
92              stateMachine.doExternalAction(new ErrorAction(
93                      new IllegalArgumentException("Invalid transition")));
94          }
95      }
96  
97      @Override
98      public void doAction(StateMachine stateMachine, RmcpMessage message) {
99          if(ProtocolDecoder.decodeAuthenticationType(message) != AuthenticationType.RMCPPlus) {
100             return;    //this isn't IPMI v2.0 message so we ignore it
101         }
102         if(Protocolv20Decoder.decodeSessionID(message) != 0){
103             return;    //this isn't sessionless message so we drop it
104         }
105         if (Protocolv20Decoder.decodePayloadType(message.getData()[1]) != PayloadType.Ipmi) {
106             return;
107         }
108         Protocolv20Decoder decoder = new Protocolv20Decoder(
109                 CipherSuite.getEmpty());
110         if(decoder.decodeAuthentication(message.getData()[1])) {
111             return;    //message is authenticated so it does belong to the other session
112         }
113         IpmiMessage ipmiMessage = null;
114         try {
115             ipmiMessage = decoder.decode(message);
116             GetChannelCipherSuites suites = new GetChannelCipherSuites();
117             if (suites.isCommandResponse(ipmiMessage)
118                     && TypeConverter.byteToInt(((IpmiLanResponse) ipmiMessage
119                             .getPayload()).getSequenceNumber()) == tag) {
120                 stateMachine.doExternalAction(new ResponseAction(suites
121                         .getResponseData(ipmiMessage)));
122             }
123         } catch (Exception e) {
124             stateMachine.doExternalAction(new ErrorAction(e));
125         }
126     }
127 
128 }