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
-
Method Summary
Modifier and TypeMethodDescriptionfinal AgentImpl
Gets a locked copy of this agent.static AgentImpl
createFromXml
(File xmlFile) Factory: Create an agent from its XML file representation.static AgentImpl
Factory: Create an agent from its XML input stream representation.static AgentImpl
createFromXml
(String xml) Factory: Create an agent from its XML string representation.static AgentImpl
createFromXml
(Element rootElement) Factory: Create an agent from its XML representation.Uses theCryptoTools
to create ajava.security.Signature
and initializes the object for signing with the agent's private key.decryptSymmetricKey
(byte[] crypted) Uses theCryptoTools
to decrypt the passed crypted content with the agent's private key.void
Encrypts the private key into a byte array with strong encryption based on a passphrase.boolean
protected String
Gets the private key encrypted and encoded in base64.The agent idReturns the id of this agent.Gets the node, this agent is running at.boolean
isLocked()
Returns whether the agent is locked or not.void
(Re-)Lock the private key.void
Notifies this agent that it has been registered at a node.void
Notifies this agent of unregistering from a node.abstract void
receiveMessage
(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 theCryptoTools
to sign the passed data with the agent's private key.void
Unlocks the private key.Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods 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:Agent
Returns whether the agent is locked or not. -
getIdentifier
Description copied from interface:Agent
The agent id- Specified by:
getIdentifier
in 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 theMediator
class.- Specified by:
getResponsibleForAgentSafeId
in 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 theCryptoTools
to 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
SecretKey
decrypted 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 theCryptoTools
to create ajava.security.Signature
and 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 yetInvalidKeyException
NoSuchAlgorithmException
-
signContent
Uses theCryptoTools
to 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:
receiveMessage
in 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:
notifyUnregister
in 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:
notifyRegistrationTo
in 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 ofMonitoringAgent
s 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 ofMonitoringAgent
s 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 ofMonitoringAgent
s 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 ofMonitoringAgent
s is not supported.- Parameters:
rootElement
-- Returns:
- an agent
- Throws:
MalformedXMLException
-
equals
-