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, StateMachineEvent machineEvent) {
72  		if (machineEvent instanceof Timeout) {
73  			stateMachine.setCurrent(new Uninitialized());
74  		} else if (machineEvent instanceof GetChannelCipherSuitesPending) {
75  			GetChannelCipherSuitesPending event = (GetChannelCipherSuitesPending) machineEvent;
76  			GetChannelCipherSuites cipherSuites = new GetChannelCipherSuites(
77  					TypeConverter.intToByte(0xE),
78  					TypeConverter.intToByte(index + 1));
79  			try {
80  				tag = event.getSequenceNumber();
81  				stateMachine
82  						.sendMessage(Encoder.encode(new Protocolv20Encoder(), cipherSuites, event.getSequenceNumber(), 0, 0));
83  				++index;
84  			} catch (Exception e) {
85  				stateMachine.setCurrent(new Uninitialized());
86  				stateMachine.doExternalAction(new ErrorAction(e));
87  			}
88  		} else if (machineEvent instanceof DefaultAck) {
89  			stateMachine.setCurrent(new Ciphers());
90  		} else {
91  			stateMachine.doExternalAction(new ErrorAction(new IllegalArgumentException("Invalid transition")));
92  		}
93  	}
94  
95  	@Override
96  	public void doAction(StateMachine stateMachine, RmcpMessage message) {
97  		if (ProtocolDecoder.decodeAuthenticationType(message) != AuthenticationType.RMCPPlus) {
98  			return; // this isn't IPMI v2.0 message so we ignore it
99  		}
100 		if (Protocolv20Decoder.decodeSessionID(message) != 0) {
101 			return; // this isn't sessionless message so we drop it
102 		}
103 		if (Protocolv20Decoder.decodePayloadType(message.getData()[1]) != PayloadType.Ipmi) {
104 			return;
105 		}
106 		Protocolv20Decoder decoder = new Protocolv20Decoder(CipherSuite.getEmpty());
107 		if (decoder.decodeAuthentication(message.getData()[1])) {
108 			return; // message is authenticated so it does belong to the other session
109 		}
110 		IpmiMessage ipmiMessage = null;
111 		try {
112 			ipmiMessage = decoder.decode(message);
113 			GetChannelCipherSuites suites = new GetChannelCipherSuites();
114 			if (suites.isCommandResponse(ipmiMessage)
115 					&& TypeConverter.byteToInt(((IpmiLanResponse) ipmiMessage.getPayload()).getSequenceNumber()) == tag) {
116 				stateMachine.doExternalAction(new ResponseAction(suites.getResponseData(ipmiMessage)));
117 			}
118 		} catch (Exception e) {
119 			stateMachine.setCurrent(new Uninitialized());
120 			stateMachine.doExternalAction(new ErrorAction(e));
121 		}
122 	}
123 
124 }