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.commands.session.Rakp1;
26  import org.metricshub.ipmi.core.coding.commands.session.Rakp1ResponseData;
27  import org.metricshub.ipmi.core.coding.commands.session.Rakp3;
28  import org.metricshub.ipmi.core.coding.payload.PlainMessage;
29  import org.metricshub.ipmi.core.coding.protocol.AuthenticationType;
30  import org.metricshub.ipmi.core.coding.protocol.IpmiMessage;
31  import org.metricshub.ipmi.core.coding.protocol.PayloadType;
32  import org.metricshub.ipmi.core.coding.protocol.decoder.PlainCommandv20Decoder;
33  import org.metricshub.ipmi.core.coding.protocol.decoder.ProtocolDecoder;
34  import org.metricshub.ipmi.core.coding.protocol.decoder.Protocolv20Decoder;
35  import org.metricshub.ipmi.core.coding.rmcp.RmcpMessage;
36  import org.metricshub.ipmi.core.coding.security.CipherSuite;
37  import org.metricshub.ipmi.core.common.TypeConverter;
38  import org.metricshub.ipmi.core.sm.StateMachine;
39  import org.metricshub.ipmi.core.sm.actions.ErrorAction;
40  import org.metricshub.ipmi.core.sm.actions.ResponseAction;
41  import org.metricshub.ipmi.core.sm.events.DefaultAck;
42  import org.metricshub.ipmi.core.sm.events.StateMachineEvent;
43  import org.metricshub.ipmi.core.sm.events.Timeout;
44  
45  /**
46   * At this point of session challenge, RAKP Message 3 was sent,
47   * {@link StateMachine} is waiting for RAKP Message 4.<br>
48   * Transition to:
49   * <ul>
50   * <li>{@link Rakp3Complete} on {@link DefaultAck}</li>
51   * <li>{@link Authcap} on {@link Timeout}</li>
52   * </ul>
53   */
54  public class Rakp3Waiting extends State {
55  
56      private Rakp1 rakp1;
57      private Rakp1ResponseData rakp1ResponseData;
58      private CipherSuite cipherSuite;
59      private int tag;
60  
61      /**
62       * Initiates state.
63       *
64       * @param rakp1
65       *            - the {@link Rakp1} message that was sent earlier in the
66       *            authentification process.
67       * @param rakp1ResponseData
68       *            - the {@link Rakp1ResponseData} that was received earlier in
69       *            the authentification process.
70       * @param cipherSuite
71       *            - the {@link CipherSuite} used during this session.
72       */
73      public Rakp3Waiting(int tag, Rakp1 rakp1,
74              Rakp1ResponseData rakp1ResponseData, CipherSuite cipherSuite) {
75          this.rakp1 = rakp1;
76          this.rakp1ResponseData = rakp1ResponseData;
77          this.cipherSuite = cipherSuite;
78          this.tag = tag;
79      }
80  
81      @Override
82      public void doTransition(StateMachine stateMachine,
83              StateMachineEvent machineEvent) {
84          if (machineEvent instanceof DefaultAck) {
85              stateMachine.setCurrent(new Rakp3Complete());
86          } else if (machineEvent instanceof Timeout) {
87              stateMachine.setCurrent(new Authcap());
88          } else {
89              stateMachine.doExternalAction(new ErrorAction(
90                      new IllegalArgumentException("Invalid transition")));
91          }
92      }
93  
94      @Override
95      public void doAction(StateMachine stateMachine, RmcpMessage message) {
96          if (ProtocolDecoder.decodeAuthenticationType(message) != AuthenticationType.RMCPPlus) {
97              return; // this isn't IPMI v2.0 message so we ignore it
98          }
99          PlainCommandv20Decoder decoder = new PlainCommandv20Decoder(
100                 CipherSuite.getEmpty());
101         if (Protocolv20Decoder.decodePayloadType(message.getData()[1]) != PayloadType.Rakp4) {
102             return;
103         }
104 
105         IpmiMessage ipmiMessage = null;
106         Rakp3 rakp3 = new Rakp3(cipherSuite, rakp1, rakp1ResponseData);
107         try {
108             ipmiMessage = decoder.decode(message);
109             if (rakp3.isCommandResponse(ipmiMessage)
110                     && TypeConverter.byteToInt(((PlainMessage) ipmiMessage
111                             .getPayload()).getPayloadData()[0]) == tag) {
112                 stateMachine.doExternalAction(new ResponseAction(rakp3
113                         .getResponseData(ipmiMessage)));
114             }
115         } catch (Exception e) {
116             stateMachine.doExternalAction(new ErrorAction(e));
117         }
118     }
119 
120 }