Package i5.las2peer.security
Class AgentImpl
java.lang.Object
i5.las2peer.security.AgentImpl
- All Implemented Interfaces:
Agent,MessageReceiver,XmlAble,Cloneable
- Direct Known Subclasses:
AnonymousAgentImpl,GroupAgentImpl,PassphraseAgentImpl,TestAgent
public abstract class AgentImpl
extends Object
implements Agent, XmlAble, Cloneable, MessageReceiver
An Agent is the basic acting entity in the las2peer network. At the moment, an agent can represent a simple user, a
group, a service or a monitoring agent.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal AgentImplGets a locked copy of this agent.static AgentImplcreateFromXml(File xmlFile) Factory: Create an agent from its XML file representation.static AgentImplFactory: Create an agent from its XML input stream representation.static AgentImplcreateFromXml(String xml) Factory: Create an agent from its XML string representation.static AgentImplcreateFromXml(Element rootElement) Factory: Create an agent from its XML representation.Uses theCryptoToolsto create ajava.security.Signatureand initializes the object for signing with the agent's private key.decryptSymmetricKey(byte[] crypted) Uses theCryptoToolsto decrypt the passed crypted content with the agent's private key.voidEncrypts the private key into a byte array with strong encryption based on a passphrase.booleanprotected StringGets the private key encrypted and encoded in base64.The agent idReturns the id of this agent.Gets the node, this agent is running at.booleanisLocked()Returns whether the agent is locked or not.void(Re-)Lock the private key.voidNotifies this agent that it has been registered at a node.voidNotifies this agent of unregistering from a node.abstract voidreceiveMessage(Message message, AgentContext c) Hook to be called by the node where this agent is registered to, when the node receives a message destined to this agent.byte[]signContent(byte[] plainData) Uses theCryptoToolsto sign the passed data with the agent's private key.voidUnlocks the private key.Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface i5.las2peer.serialization.XmlAble
toXmlString
-
Constructor Details
-
AgentImpl
protected AgentImpl()Creates an empty agent. -
AgentImpl
Creates a new agent.- Parameters:
pair- The private public keypair to use for this agentkey- The secret key to encrypt the private key- Throws:
AgentOperationFailedException- If the private key can not be encrypted
-
AgentImpl
Creates a new agent.- Parameters:
publicKey- The public key to use for this agentencryptedPrivate- The encrypted private key to use for this agent
-
-
Method Details
-
lockPrivateKey
public void lockPrivateKey()(Re-)Lock the private key. -
unlockPrivateKey
public void unlockPrivateKey(SecretKey key) throws AgentOperationFailedException, AgentAccessDeniedException Unlocks the private key.- Parameters:
key- A key that is used to unlock the agents private key.- Throws:
AgentOperationFailedException- If the agent's private key can not be deserialized.AgentAccessDeniedException
-
encryptPrivateKey
public void encryptPrivateKey(SecretKey key) throws AgentOperationFailedException, AgentLockedException Encrypts the private key into a byte array with strong encryption based on a passphrase. to unlock the key- Parameters:
key- A key that is used to encrypt the agents private key.- Throws:
AgentOperationFailedException- If an issue with the given key occurs.AgentLockedException- If the given agent is not unlocked
-
isLocked
public boolean isLocked()Description copied from interface:AgentReturns whether the agent is locked or not. -
getIdentifier
Description copied from interface:AgentThe agent id- Specified by:
getIdentifierin interfaceAgent- Returns:
- The agent id.
-
getResponsibleForAgentSafeId
Returns the id of this agent. This method is only implemented, since an Agent is also aMessageReceiver, thus has to implement this method. It was written for theMediatorclass.- Specified by:
getResponsibleForAgentSafeIdin interfaceMessageReceiver- Returns:
- id of the agent
-
getPublicKey
- Returns:
- the cryptographic public key of this agent
-
decryptSymmetricKey
public SecretKey decryptSymmetricKey(byte[] crypted) throws AgentLockedException, SerializationException, CryptoException Uses theCryptoToolsto decrypt the passed crypted content with the agent's private key.- Parameters:
crypted- The encrypted content that is decrypted using the agents private key.- Returns:
- Returns a
SecretKeydecrypted from the crypted input and the agent's private key - Throws:
AgentLockedException- the private key has not been unlocked yetCryptoException- If an issue occurs with decryption.SerializationException- If an issue occurs with deserializing the given and decrypted data.
-
createSignature
public Signature createSignature() throws InvalidKeyException, AgentLockedException, NoSuchAlgorithmExceptionUses theCryptoToolsto create ajava.security.Signatureand initializes the object for signing with the agent's private key.- Returns:
- a
java.security.Signature - Throws:
AgentLockedException- the private key has not been unlocked yetInvalidKeyExceptionNoSuchAlgorithmException
-
signContent
Uses theCryptoToolsto sign the passed data with the agent's private key.- Parameters:
plainData- The plain data to sign- Returns:
- Returns a signed version of the input
- Throws:
AgentLockedException- the private key has not been unlocked yetCryptoException
-
getEncodedPrivate
Gets the private key encrypted and encoded in base64. mainly fortoXmlString()methods of subclasses- Returns:
- encoded version or the private key
-
receiveMessage
Hook to be called by the node where this agent is registered to, when the node receives a message destined to this agent.- Specified by:
receiveMessagein interfaceMessageReceiver- Parameters:
message-c-- Throws:
MessageException
-
cloneLocked
Gets a locked copy of this agent.- Returns:
- a locked clone of this agent
- Throws:
CloneNotSupportedException
-
notifyUnregister
public void notifyUnregister()Notifies this agent of unregistering from a node.- Specified by:
notifyUnregisterin interfaceMessageReceiver
-
notifyRegistrationTo
Notifies this agent that it has been registered at a node. May be overridden in implementing classes. Make sure, overriding methods do a call of this method!- Specified by:
notifyRegistrationToin interfaceMessageReceiver- Parameters:
n-- Throws:
AgentException
-
getRunningAtNode
Gets the node, this agent is running at.- Returns:
- the node, this agent is running at
-
createFromXml
Factory: Create an agent from its XML file representation. Depending on the type attribute of the root node, the type will be aUserAgentImpl,GroupAgentImpl,ServiceAgentImpl. Creation ofMonitoringAgents is not supported.- Parameters:
xmlFile-- Returns:
- an agent
- Throws:
MalformedXMLException
-
createFromXml
Factory: Create an agent from its XML string representation. Depending on the type attribute of the root node, the type will be aUserAgentImpl,GroupAgentImpl,ServiceAgentImpl. Creation ofMonitoringAgents is not supported.- Parameters:
xml-- Returns:
- an agent
- Throws:
MalformedXMLException
-
createFromXml
Factory: Create an agent from its XML input stream representation. Depending on the type attribute of the root node, the type will be aUserAgentImpl,GroupAgentImpl,ServiceAgentImpl. Creation ofMonitoringAgents is not supported.- Parameters:
is-- Returns:
- an agent
- Throws:
MalformedXMLException
-
createFromXml
Factory: Create an agent from its XML representation. Depending on the type attribute of the root node, the type will be aUserAgentImpl,GroupAgentImpl,ServiceAgentImpl. Creation ofMonitoringAgents is not supported.- Parameters:
rootElement-- Returns:
- an agent
- Throws:
MalformedXMLException
-
equals
-