1 /* 2 * Copyright 2018 Google LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef FCP_SECAGG_CLIENT_SECAGG_CLIENT_STATE_H_ 18 #define FCP_SECAGG_CLIENT_SECAGG_CLIENT_STATE_H_ 19 20 #include <memory> 21 #include <string> 22 #include <vector> 23 24 #include "fcp/base/monitoring.h" 25 #include "fcp/secagg/client/send_to_server_interface.h" 26 #include "fcp/secagg/client/state_transition_listener_interface.h" 27 #include "fcp/secagg/shared/input_vector_specification.h" 28 #include "fcp/secagg/shared/secagg_messages.pb.h" 29 #include "fcp/secagg/shared/secagg_vector.h" 30 31 namespace fcp { 32 namespace secagg { 33 34 // This is an abstract class which is the parent of the other SecAggClient*State 35 // classes. It should not be instantiated directly. Default versions of all the 36 // methods declared here are provided for use by states which do not expect, and 37 // therefore do not implement, those methods. 38 39 class SecAggClientState { 40 public: 41 // Initiates the protocol by computing its first message and sending it to the 42 // server. If called in a valid state, returns the new State. Otherwise, 43 // returns an error Status with code PRECONDITION_FAILED. 44 virtual StatusOr<std::unique_ptr<SecAggClientState> > Start(); 45 46 // Handles the received message in a way consistent with the current state. 47 // If called from a state expecting a message, returns the new State. If the 48 // message was of the right type but had invalid contents, the new State will 49 // be a SecAggClientAbortState. 50 // If the state was not expecting a message of this type at all, returns an 51 // error Status with code PRECONDITION_FAILED. 52 virtual StatusOr<std::unique_ptr<SecAggClientState> > HandleMessage( 53 const ServerToClientWrapperMessage& message); 54 55 // Sets the input of this client for this protocol session. If successful, 56 // returns the new state. If the input does not match the specification, 57 // returns an error Status with code INVALID_ARGUMENT. 58 // If the client's state was not ready for an input to be set, returns an 59 // error Status with code PRECONDITION_FAILED. 60 virtual StatusOr<std::unique_ptr<SecAggClientState> > SetInput( 61 std::unique_ptr<SecAggVectorMap> input_map); 62 63 // Aborts the protocol for the specified reason. Returns the new state. If the 64 // protocol was already aborted or completed, instead returns an error Status 65 // with code PRECONDITION_FAILED. 66 virtual StatusOr<std::unique_ptr<SecAggClientState> > Abort( 67 const std::string& reason); 68 69 // Returns true if the current state is Abort, false else. 70 ABSL_MUST_USE_RESULT virtual bool IsAborted() const; 71 72 // Returns true if the current state is ProtocolCompleted, false else. 73 ABSL_MUST_USE_RESULT virtual bool IsCompletedSuccessfully() const; 74 75 // Returns the error message, if the current state is an abort state. If not, 76 // returns an error Status with code PRECONDITION_FAILED. 77 ABSL_MUST_USE_RESULT virtual StatusOr<std::string> ErrorMessage() const; 78 79 // Returns the name of the current state, as a string. 80 ABSL_MUST_USE_RESULT virtual std::string StateName() const = 0; 81 82 virtual ~SecAggClientState() = default; 83 84 protected: 85 // The object that sends messages to the server. 86 std::unique_ptr<SendToServerInterface> sender_; 87 // A listener for state transitions. 88 std::unique_ptr<StateTransitionListenerInterface> transition_listener_; 89 // State type. 90 ClientState state_; 91 92 // SecAggClientState should never be instantiated directly. 93 SecAggClientState( 94 std::unique_ptr<SendToServerInterface> sender, 95 std::unique_ptr<StateTransitionListenerInterface> transition_listener, 96 ClientState state); 97 98 // Validates an input map by returning true if all SecAggVectors match their 99 // corresponding InputVectorSpecifications, and false otherwise. 100 bool ValidateInput( 101 const SecAggVectorMap& input_map, 102 const std::vector<InputVectorSpecification>& input_vector_specs); 103 }; 104 105 } // namespace secagg 106 } // namespace fcp 107 108 #endif // FCP_SECAGG_CLIENT_SECAGG_CLIENT_STATE_H_ 109